From 01292d05d9f85504a20f020e0d06161a06124b32 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Thu, 26 Feb 2026 02:43:43 +0800 Subject: [PATCH] Do not attempt to evaluate obligations on PointeeSized goals --- .../traits/fulfillment_errors.rs | 22 +++++++++++-------- .../similarly-named-trait-pointee-sized.rs | 11 ++++++++++ ...similarly-named-trait-pointee-sized.stderr | 21 ++++++++++++++++++ ...amed_trait.rs => similarly-named-trait.rs} | 0 ...it.stderr => similarly-named-trait.stderr} | 16 +++++++------- 5 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 tests/ui/traits/similarly-named-trait-pointee-sized.rs create mode 100644 tests/ui/traits/similarly-named-trait-pointee-sized.stderr rename tests/ui/traits/{similarly_named_trait.rs => similarly-named-trait.rs} (100%) rename tests/ui/traits/{similarly_named_trait.stderr => similarly-named-trait.stderr} (81%) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index f4a9b6635c91b..597882320ec33 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -2632,15 +2632,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { trait_def_id != def_id && trait_name == self.tcx.item_name(def_id) && trait_has_same_params(def_id) - && self.predicate_must_hold_modulo_regions(&Obligation::new( - self.tcx, - obligation.cause.clone(), - obligation.param_env, - trait_pred.map_bound(|tr| ty::TraitPredicate { - trait_ref: ty::TraitRef::new(self.tcx, def_id, tr.trait_ref.args), - ..tr - }), - )) + // `PointeeSized` is special -- it's a very weak/top sizedness marker that's + // effectively implemented for all types, and most importantly isn't lowered + // to the trait solver, so we skip doing any semantic checking for it. + && (self.tcx.is_lang_item(def_id, LangItem::PointeeSized) + || self.predicate_must_hold_modulo_regions(&Obligation::new( + self.tcx, + obligation.cause.clone(), + obligation.param_env, + trait_pred.map_bound(|tr| ty::TraitPredicate { + trait_ref: ty::TraitRef::new(self.tcx, def_id, tr.trait_ref.args), + ..tr + }), + ))) }) { err.note(format!( "`{}` implements similarly named trait `{}`, but not `{}`", diff --git a/tests/ui/traits/similarly-named-trait-pointee-sized.rs b/tests/ui/traits/similarly-named-trait-pointee-sized.rs new file mode 100644 index 0000000000000..064a401f8a879 --- /dev/null +++ b/tests/ui/traits/similarly-named-trait-pointee-sized.rs @@ -0,0 +1,11 @@ +#![feature(sized_hierarchy)] +trait PointeeSized {} //~ HELP this trait has no implementations, consider adding one + +fn require_trait() {} //~ NOTE required by a bound in `require_trait` + //~| NOTE required by this bound in `require_trait` + +fn main() { + require_trait::(); //~ ERROR the trait bound `i32: PointeeSized` is not satisfied + //~^ NOTE the trait `PointeeSized` is not implemented for `i32` + //~| NOTE `i32` implements similarly named trait `std::marker::PointeeSized`, but not `PointeeSized` +} diff --git a/tests/ui/traits/similarly-named-trait-pointee-sized.stderr b/tests/ui/traits/similarly-named-trait-pointee-sized.stderr new file mode 100644 index 0000000000000..1d77de43d009d --- /dev/null +++ b/tests/ui/traits/similarly-named-trait-pointee-sized.stderr @@ -0,0 +1,21 @@ +error[E0277]: the trait bound `i32: PointeeSized` is not satisfied + --> $DIR/similarly-named-trait-pointee-sized.rs:8:21 + | +LL | require_trait::(); + | ^^^ the trait `PointeeSized` is not implemented for `i32` + | + = note: `i32` implements similarly named trait `std::marker::PointeeSized`, but not `PointeeSized` +help: this trait has no implementations, consider adding one + --> $DIR/similarly-named-trait-pointee-sized.rs:2:1 + | +LL | trait PointeeSized {} + | ^^^^^^^^^^^^^^^^^^ +note: required by a bound in `require_trait` + --> $DIR/similarly-named-trait-pointee-sized.rs:4:21 + | +LL | fn require_trait() {} + | ^^^^^^^^^^^^ required by this bound in `require_trait` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/similarly_named_trait.rs b/tests/ui/traits/similarly-named-trait.rs similarity index 100% rename from tests/ui/traits/similarly_named_trait.rs rename to tests/ui/traits/similarly-named-trait.rs diff --git a/tests/ui/traits/similarly_named_trait.stderr b/tests/ui/traits/similarly-named-trait.stderr similarity index 81% rename from tests/ui/traits/similarly_named_trait.stderr rename to tests/ui/traits/similarly-named-trait.stderr index 2432e1ffbbe50..bd85c0d70a43e 100644 --- a/tests/ui/traits/similarly_named_trait.stderr +++ b/tests/ui/traits/similarly-named-trait.stderr @@ -1,5 +1,5 @@ error[E0277]: the trait bound `St: Trait` is not satisfied - --> $DIR/similarly_named_trait.rs:20:10 + --> $DIR/similarly-named-trait.rs:20:10 | LL | func(m::St); | ---- ^^^^^ unsatisfied trait bound @@ -7,24 +7,24 @@ LL | func(m::St); | required by a bound introduced by this call | help: the trait `Trait` is not implemented for `St` - --> $DIR/similarly_named_trait.rs:7:5 + --> $DIR/similarly-named-trait.rs:7:5 | LL | pub struct St; | ^^^^^^^^^^^^^ = note: `St` implements similarly named trait `m::Trait`, but not `Trait` help: this trait has no implementations, consider adding one - --> $DIR/similarly_named_trait.rs:1:1 + --> $DIR/similarly-named-trait.rs:1:1 | LL | trait Trait {} | ^^^^^^^^^^^ note: required by a bound in `func` - --> $DIR/similarly_named_trait.rs:13:12 + --> $DIR/similarly-named-trait.rs:13:12 | LL | fn func(_: T) {} | ^^^^^ required by this bound in `func` error[E0277]: the trait bound `St: TraitWithParam` is not satisfied - --> $DIR/similarly_named_trait.rs:24:11 + --> $DIR/similarly-named-trait.rs:24:11 | LL | func2(m::St); | ----- ^^^^^ unsatisfied trait bound @@ -32,18 +32,18 @@ LL | func2(m::St); | required by a bound introduced by this call | help: the trait `TraitWithParam` is not implemented for `St` - --> $DIR/similarly_named_trait.rs:7:5 + --> $DIR/similarly-named-trait.rs:7:5 | LL | pub struct St; | ^^^^^^^^^^^^^ = note: `St` implements similarly named trait `m::TraitWithParam`, but not `TraitWithParam` help: this trait has no implementations, consider adding one - --> $DIR/similarly_named_trait.rs:2:1 + --> $DIR/similarly-named-trait.rs:2:1 | LL | trait TraitWithParam {} | ^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `func2` - --> $DIR/similarly_named_trait.rs:16:13 + --> $DIR/similarly-named-trait.rs:16:13 | LL | fn func2> (_: T) {} | ^^^^^^^^^^^^^^^^^ required by this bound in `func2`