about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-03-31 01:14:46 +0200
committerGitHub <noreply@github.com>2021-03-31 01:14:46 +0200
commit7d888d100fffa0040ac0b26f8839b24f64429c4b (patch)
treec21be858f980a32438a07ffcd57edc2a1166197a
parent7391124154094b9177e0448026aa0d6dd5c1a48a (diff)
parent7e6fd406148b97d9e9af4097b3425d5cfab1b217 (diff)
downloadrust-7d888d100fffa0040ac0b26f8839b24f64429c4b.tar.gz
rust-7d888d100fffa0040ac0b26f8839b24f64429c4b.zip
Rollup merge of #83654 - JohnTitor:issue-83606, r=estebank
Do not emit a suggestion that causes the E0632 error

Fixes #83606
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs32
-rw-r--r--src/test/ui/inference/issue-83606.rs10
-rw-r--r--src/test/ui/inference/issue-83606.stderr11
3 files changed, 47 insertions, 6 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
index d533e267fd7..d9a1193aac4 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
@@ -287,6 +287,7 @@ pub struct InferenceDiagnosticsData {
 pub struct InferenceDiagnosticsParentData {
     pub prefix: &'static str,
     pub name: String,
+    pub def_id: DefId,
 }
 
 pub enum UnderspecifiedArgKind {
@@ -328,6 +329,7 @@ impl InferenceDiagnosticsParentData {
         Some(InferenceDiagnosticsParentData {
             prefix: tcx.def_kind(parent_def_id).descr(parent_def_id),
             name: parent_name,
+            def_id: parent_def_id,
         })
     }
 }
@@ -754,12 +756,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
             if let (UnderspecifiedArgKind::Const { .. }, Some(parent_data)) =
                 (&arg_data.kind, &arg_data.parent)
             {
-                err.span_suggestion_verbose(
-                    span,
-                    "consider specifying the const argument",
-                    format!("{}::<{}>", parent_data.name, arg_data.name),
-                    Applicability::MaybeIncorrect,
-                );
+                let has_impl_trait =
+                    self.tcx.generics_of(parent_data.def_id).params.iter().any(|param| {
+                        matches!(
+                            param.kind,
+                            ty::GenericParamDefKind::Type {
+                                synthetic: Some(
+                                    hir::SyntheticTyParamKind::ImplTrait
+                                        | hir::SyntheticTyParamKind::FromAttr,
+                                ),
+                                ..
+                            }
+                        )
+                    });
+
+                // (#83606): Do not emit a suggestion if the parent has an `impl Trait`
+                // as an argument otherwise it will cause the E0282 error.
+                if !has_impl_trait {
+                    err.span_suggestion_verbose(
+                        span,
+                        "consider specifying the const argument",
+                        format!("{}::<{}>", parent_data.name, arg_data.name),
+                        Applicability::MaybeIncorrect,
+                    );
+                }
             }
 
             err.span_label(
diff --git a/src/test/ui/inference/issue-83606.rs b/src/test/ui/inference/issue-83606.rs
new file mode 100644
index 00000000000..be56a3020cc
--- /dev/null
+++ b/src/test/ui/inference/issue-83606.rs
@@ -0,0 +1,10 @@
+// Regression test for #83606.
+
+fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
+    [0; N]
+}
+
+fn main() {
+    let _ = foo("foo"); //<- Do not suggest `foo::<N>("foo");`!
+    //~^ ERROR: type annotations needed for `[usize; _]`
+}
diff --git a/src/test/ui/inference/issue-83606.stderr b/src/test/ui/inference/issue-83606.stderr
new file mode 100644
index 00000000000..65f3336b935
--- /dev/null
+++ b/src/test/ui/inference/issue-83606.stderr
@@ -0,0 +1,11 @@
+error[E0282]: type annotations needed for `[usize; _]`
+  --> $DIR/issue-83606.rs:8:13
+   |
+LL |     let _ = foo("foo"); //<- Do not suggest `foo::<N>("foo");`!
+   |         -   ^^^ cannot infer the value of const parameter `N` declared on the function `foo`
+   |         |
+   |         consider giving this pattern the explicit type `[usize; _]`, where the type parameter `N` is specified
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.