Skip to content

Do not register Self: AutoTrait when confirming auto trait (in old solver) #138249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,29 +463,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);

assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
let trait_ref =
self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
let trait_obligations = self.impl_or_trait_obligations(
Copy link
Member Author

@compiler-errors compiler-errors Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be very clear, this impl_or_trait_obligations returns a single predicate, which is this one here:

if tcx.is_trait(def_id) {
// For traits, add `Self: Trait` predicate. This is
// not part of the predicates that a user writes, but it
// is something that one must prove in order to invoke a
// method or project an associated type.
//
// In the chalk setup, this predicate is not part of the
// "predicates" for a trait item. But it is useful in
// rustc because if you directly (e.g.) invoke a trait
// method like `Trait::method(...)`, you must naturally
// prove that the trait applies to the types that were
// used, and adding the predicate into this list ensures
// that this is done.
//
// We use a DUMMY_SP here as a way to signal trait bounds that come
// from the trait itself that *shouldn't* be shown as the source of
// an obligation and instead be skipped. Otherwise we'd use
// `tcx.def_span(def_id);`
let span = DUMMY_SP;
result.predicates = tcx.arena.alloc_from_iter(
result
.predicates
.iter()
.copied()
.chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
);
}

Arguably that predicate shouldn't even be in the predicates_of and perhaps should be put into the param_env of traits and trait items... but that seems a lot harder to do.

And we would still need to make sure to add the predicate in when calling methods and stuff.

&cause,
obligation.recursion_depth + 1,
obligation.param_env,
trait_def_id,
trait_ref.args,
obligation.predicate,
);

let mut obligations = self.collect_predicates_for_types(
let obligations = self.collect_predicates_for_types(
obligation.param_env,
cause,
obligation.recursion_depth + 1,
trait_def_id,
nested,
);

// Adds the predicates from the trait. Note that this contains a `Self: Trait`
// predicate as usual. It won't have any effect since auto traits are coinductive.
obligations.extend(trait_obligations);

debug!(?obligations, "vtable_auto_impl");

obligations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
struct NoClone;

fn main() {
let (a, b) = copy(NoClone); //~ ERROR
let (a, b) = copy(NoClone);
println!("{:?} {:?}", a, b);
}
29 changes: 2 additions & 27 deletions tests/ui/traits/inductive-overflow/supertrait-auto-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@ LL | auto trait Magic: Copy {}
| |
| auto traits cannot have super traits or lifetime bounds

error[E0277]: the trait bound `NoClone: Magic` is not satisfied
--> $DIR/supertrait-auto-trait.rs:16:23
|
LL | let (a, b) = copy(NoClone);
| ---- ^^^^^^^ the trait `Copy` is not implemented for `NoClone`
| |
| required by a bound introduced by this call
|
note: required for `NoClone` to implement `Magic`
--> $DIR/supertrait-auto-trait.rs:8:12
|
LL | auto trait Magic: Copy {}
| ^^^^^
note: required by a bound in `copy`
--> $DIR/supertrait-auto-trait.rs:10:12
|
LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
| ^^^^^ required by this bound in `copy`
help: consider annotating `NoClone` with `#[derive(Copy)]`
|
LL + #[derive(Copy)]
LL | struct NoClone;
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0277, E0568.
For more information about an error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0568`.
Loading