Skip to content

Commit 7d888d1

Browse files
authored
Rollup merge of #83654 - JohnTitor:issue-83606, r=estebank
Do not emit a suggestion that causes the E0632 error Fixes #83606
2 parents 7391124 + 7e6fd40 commit 7d888d1

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub struct InferenceDiagnosticsData {
287287
pub struct InferenceDiagnosticsParentData {
288288
pub prefix: &'static str,
289289
pub name: String,
290+
pub def_id: DefId,
290291
}
291292

292293
pub enum UnderspecifiedArgKind {
@@ -328,6 +329,7 @@ impl InferenceDiagnosticsParentData {
328329
Some(InferenceDiagnosticsParentData {
329330
prefix: tcx.def_kind(parent_def_id).descr(parent_def_id),
330331
name: parent_name,
332+
def_id: parent_def_id,
331333
})
332334
}
333335
}
@@ -754,12 +756,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
754756
if let (UnderspecifiedArgKind::Const { .. }, Some(parent_data)) =
755757
(&arg_data.kind, &arg_data.parent)
756758
{
757-
err.span_suggestion_verbose(
758-
span,
759-
"consider specifying the const argument",
760-
format!("{}::<{}>", parent_data.name, arg_data.name),
761-
Applicability::MaybeIncorrect,
762-
);
759+
let has_impl_trait =
760+
self.tcx.generics_of(parent_data.def_id).params.iter().any(|param| {
761+
matches!(
762+
param.kind,
763+
ty::GenericParamDefKind::Type {
764+
synthetic: Some(
765+
hir::SyntheticTyParamKind::ImplTrait
766+
| hir::SyntheticTyParamKind::FromAttr,
767+
),
768+
..
769+
}
770+
)
771+
});
772+
773+
// (#83606): Do not emit a suggestion if the parent has an `impl Trait`
774+
// as an argument otherwise it will cause the E0282 error.
775+
if !has_impl_trait {
776+
err.span_suggestion_verbose(
777+
span,
778+
"consider specifying the const argument",
779+
format!("{}::<{}>", parent_data.name, arg_data.name),
780+
Applicability::MaybeIncorrect,
781+
);
782+
}
763783
}
764784

765785
err.span_label(

src/test/ui/inference/issue-83606.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for #83606.
2+
3+
fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
4+
[0; N]
5+
}
6+
7+
fn main() {
8+
let _ = foo("foo"); //<- Do not suggest `foo::<N>("foo");`!
9+
//~^ ERROR: type annotations needed for `[usize; _]`
10+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed for `[usize; _]`
2+
--> $DIR/issue-83606.rs:8:13
3+
|
4+
LL | let _ = foo("foo"); //<- Do not suggest `foo::<N>("foo");`!
5+
| - ^^^ cannot infer the value of const parameter `N` declared on the function `foo`
6+
| |
7+
| consider giving this pattern the explicit type `[usize; _]`, where the type parameter `N` is specified
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)