Skip to content

Commit 2745644

Browse files
committed
Do not emit E0747 if a resolve error was already emitted
1 parent 1c737ed commit 2745644

16 files changed

+87
-237
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ fn generic_arg_mismatch_err(
6464
) => match path.res {
6565
Res::Err => {
6666
add_braces_suggestion(arg, &mut err);
67-
return err
68-
.with_primary_message("unresolved item provided when a constant was expected")
69-
.emit();
67+
err.primary_message("unresolved item provided when a constant was expected");
68+
// A resolve error will already have been emitted pointing at this.
69+
return err.delay_as_bug();
7070
}
7171
Res::Def(DefKind::TyParam, src_def_id) => {
7272
if let Some(param_local_id) = param.def_id.as_local() {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
498498
None
499499
};
500500

501+
let mut delay_as_bug = false;
501502
let mut infer_subdiags = Vec::new();
502503
let mut multi_suggestions = Vec::new();
503504
match kind {
@@ -513,7 +514,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
513514
type_name: ty_to_string(self, ty, def_id),
514515
});
515516
}
516-
InferSourceKind::ClosureArg { insert_span, ty, .. } => {
517+
InferSourceKind::ClosureArg { insert_span, ty, kind, .. } => {
518+
if let PatKind::Err(_) = kind {
519+
// We will have already emitted an error about this pattern.
520+
delay_as_bug = true;
521+
}
517522
infer_subdiags.push(SourceKindSubdiag::LetLike {
518523
span: insert_span,
519524
name: String::new(),
@@ -532,7 +537,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
532537
def_id: _,
533538
generic_args,
534539
have_turbofish,
540+
hir_id,
535541
} => {
542+
if let hir::Node::PathSegment(segment) = self.tcx.hir_node(hir_id)
543+
&& let Some(args) = segment.args
544+
&& let Some(hir::GenericArg::Type(ty)) = args.args.get(argument_index)
545+
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
546+
&& let Res::Err = path.res
547+
{
548+
// We have already emitted a name resolution error.
549+
delay_as_bug = true;
550+
}
536551
let generics = self.tcx.generics_of(generics_def_id);
537552
let is_type = term.as_type().is_some();
538553

@@ -653,8 +668,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
653668
}),
654669
};
655670
*err.long_ty_path() = long_ty_path;
656-
if let InferSourceKind::ClosureArg { kind: PatKind::Err(_), .. } = kind {
657-
// We will have already emitted an error about this pattern.
671+
if silence {
672+
// We have already emitted an earlier more relevant error.
658673
err.downgrade_to_delayed_bug();
659674
}
660675
err
@@ -687,6 +702,7 @@ enum InferSourceKind<'tcx> {
687702
def_id: DefId,
688703
generic_args: &'tcx [GenericArg<'tcx>],
689704
have_turbofish: bool,
705+
hir_id: HirId,
690706
},
691707
FullyQualifiedMethodCall {
692708
receiver: &'tcx Expr<'tcx>,
@@ -751,6 +767,7 @@ struct InsertableGenericArgs<'tcx> {
751767
generics_def_id: DefId,
752768
def_id: DefId,
753769
have_turbofish: bool,
770+
hir_id: HirId,
754771
}
755772

756773
/// A visitor which searches for the "best" spot to use in the inference error.
@@ -1013,6 +1030,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10131030
generics_def_id: def_id,
10141031
def_id,
10151032
have_turbofish: false,
1033+
hir_id: expr.hir_id,
10161034
}
10171035
};
10181036
return Box::new(insertable.into_iter());
@@ -1052,6 +1070,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10521070
generics_def_id,
10531071
def_id: path.res.def_id(),
10541072
have_turbofish,
1073+
hir_id: path.segments.last().unwrap().hir_id,
10551074
}
10561075
};
10571076

@@ -1072,6 +1091,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10721091
generics_def_id,
10731092
def_id: res.def_id(),
10741093
have_turbofish,
1094+
hir_id: segment.hir_id,
10751095
})
10761096
})
10771097
.chain(last_segment_using_path_data)
@@ -1106,6 +1126,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
11061126
generics_def_id: def_id,
11071127
def_id,
11081128
have_turbofish: false,
1129+
hir_id: segment.hir_id,
11091130
}
11101131
};
11111132

@@ -1233,6 +1254,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
12331254
generics_def_id,
12341255
def_id,
12351256
have_turbofish,
1257+
hir_id,
12361258
} = args;
12371259
let generics = tcx.generics_of(generics_def_id);
12381260
if let Some(mut argument_index) = generics
@@ -1260,6 +1282,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
12601282
def_id,
12611283
generic_args,
12621284
have_turbofish,
1285+
hir_id,
12631286
},
12641287
});
12651288
}

tests/ui/const-generics/assoc_const_as_type_argument.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ fn bar<const N: usize>() {}
77
fn foo<T: Trait>() {
88
bar::<<T as Trait>::ASSOC>();
99
//~^ ERROR: expected associated type, found associated constant `Trait::ASSOC`
10-
//~| ERROR: unresolved item provided when a constant was expected
1110
}
1211

1312
fn main() {}

tests/ui/const-generics/assoc_const_as_type_argument.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ error[E0575]: expected associated type, found associated constant `Trait::ASSOC`
44
LL | bar::<<T as Trait>::ASSOC>();
55
| ^^^^^^^^^^^^^^^^^^^ not a associated type
66

7-
error[E0747]: unresolved item provided when a constant was expected
8-
--> $DIR/assoc_const_as_type_argument.rs:8:11
9-
|
10-
LL | bar::<<T as Trait>::ASSOC>();
11-
| ^^^^^^^^^^^^^^^^^^^
12-
|
13-
help: if this generic argument was intended as a const parameter, surround it with braces
14-
|
15-
LL | bar::<{ <T as Trait>::ASSOC }>();
16-
| + +
17-
18-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
198

20-
Some errors have detailed explanations: E0575, E0747.
21-
For more information about an error, try `rustc --explain E0575`.
9+
For more information about this error, try `rustc --explain E0575`.

0 commit comments

Comments
 (0)