Skip to content

Commit 919f672

Browse files
committed
Avoid unnecessary note when type has escaping bounds
1 parent 91425d0 commit 919f672

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,15 @@ fn extend_type_not_partial_eq<'tcx>(
385385
/// The type has no `PartialEq` implementation, neither manual or derived, but
386386
/// we don't have a span to point at, so we'll just add them as a `note`.
387387
without: FxHashSet<Ty<'tcx>>,
388+
/// If any of the subtypes has escaping bounds (`for<'a>`), we won't emit a note.
389+
has_escaping_bound_vars: bool,
388390
}
389391

390392
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
391393
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
392-
if let ty::Adt(def, _args) = ty.kind()
393-
&& !ty.has_escaping_bound_vars()
394-
{
394+
if ty.has_escaping_bound_vars() {
395+
self.has_escaping_bound_vars = true;
396+
} else if let ty::Adt(def, _args) = ty.kind() {
395397
let ty_def_id = def.did();
396398
let ty_def_span = self.tcx.def_span(ty_def_id);
397399
let (impls_partial_eq, derived, structural, impl_def_id) =
@@ -426,8 +428,12 @@ fn extend_type_not_partial_eq<'tcx>(
426428
adts_without_partialeq: FxHashSet::default(),
427429
manual: FxHashSet::default(),
428430
without: FxHashSet::default(),
431+
has_escaping_bound_vars: false,
429432
};
430433
v.visit_ty(ty);
434+
if v.has_escaping_bound_vars {
435+
return;
436+
}
431437
#[allow(rustc::potential_query_instability)] // Span labels will be sorted by the rendering
432438
for span in v.adts_with_manual_partialeq {
433439
err.span_note(span, "the `PartialEq` trait must be derived, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details");

tests/ui/consts/const_in_pattern/non_structural_with_escaping_bounds.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | C => (),
88
| ^ constant of non-structural type
99
|
1010
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
11-
= note: `std::alloc::Global` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
1211

1312
error: aborting due to 1 previous error
1413

0 commit comments

Comments
 (0)