Skip to content

Commit

Permalink
Rollup merge of #83654 - JohnTitor:issue-83606, r=estebank
Browse files Browse the repository at this point in the history
Do not emit a suggestion that causes the E0632 error

Fixes #83606
  • Loading branch information
Dylan-DPC authored Mar 30, 2021
2 parents 7391124 + 7e6fd40 commit 7d888d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
32 changes: 26 additions & 6 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ pub struct InferenceDiagnosticsData {
pub struct InferenceDiagnosticsParentData {
pub prefix: &'static str,
pub name: String,
pub def_id: DefId,
}

pub enum UnderspecifiedArgKind {
Expand Down Expand Up @@ -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,
})
}
}
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/inference/issue-83606.rs
Original file line number Diff line number Diff line change
@@ -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; _]`
}
11 changes: 11 additions & 0 deletions src/test/ui/inference/issue-83606.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 7d888d1

Please sign in to comment.