Skip to content

Commit 961351c

Browse files
committed
Auto merge of rust-lang#138249 - compiler-errors:auto-self, r=lcnr
Do not register `Self: AutoTrait` when confirming auto trait (in old solver) Every built-in auto impl for a trait goal like `Ty: Auto` immediately registers another obligation of `Ty: Auto` as one of its nested obligations, leading to us stressing the cycle detection machinery a lot more than we need to. This is because all traits have a `Self: Trait` predicate. To fix this, remove the call to `impl_or_trait_obligations` in `vtable_auto_impl`, since auto traits do not have where clauses. r? lcnr
2 parents 8536f20 + 3129802 commit 961351c

File tree

3 files changed

+4
-43
lines changed

3 files changed

+4
-43
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -463,29 +463,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
463463
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
464464

465465
assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
466-
let trait_ref =
467-
self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
468-
let trait_obligations = self.impl_or_trait_obligations(
469-
&cause,
470-
obligation.recursion_depth + 1,
471-
obligation.param_env,
472-
trait_def_id,
473-
trait_ref.args,
474-
obligation.predicate,
475-
);
476466

477-
let mut obligations = self.collect_predicates_for_types(
467+
let obligations = self.collect_predicates_for_types(
478468
obligation.param_env,
479469
cause,
480470
obligation.recursion_depth + 1,
481471
trait_def_id,
482472
nested,
483473
);
484474

485-
// Adds the predicates from the trait. Note that this contains a `Self: Trait`
486-
// predicate as usual. It won't have any effect since auto traits are coinductive.
487-
obligations.extend(trait_obligations);
488-
489475
debug!(?obligations, "vtable_auto_impl");
490476

491477
obligations

tests/ui/traits/inductive-overflow/supertrait-auto-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
1313
struct NoClone;
1414

1515
fn main() {
16-
let (a, b) = copy(NoClone); //~ ERROR
16+
let (a, b) = copy(NoClone);
1717
println!("{:?} {:?}", a, b);
1818
}

tests/ui/traits/inductive-overflow/supertrait-auto-trait.stderr

+2-27
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ LL | auto trait Magic: Copy {}
66
| |
77
| auto traits cannot have super traits or lifetime bounds
88

9-
error[E0277]: the trait bound `NoClone: Magic` is not satisfied
10-
--> $DIR/supertrait-auto-trait.rs:16:23
11-
|
12-
LL | let (a, b) = copy(NoClone);
13-
| ---- ^^^^^^^ the trait `Copy` is not implemented for `NoClone`
14-
| |
15-
| required by a bound introduced by this call
16-
|
17-
note: required for `NoClone` to implement `Magic`
18-
--> $DIR/supertrait-auto-trait.rs:8:12
19-
|
20-
LL | auto trait Magic: Copy {}
21-
| ^^^^^
22-
note: required by a bound in `copy`
23-
--> $DIR/supertrait-auto-trait.rs:10:12
24-
|
25-
LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
26-
| ^^^^^ required by this bound in `copy`
27-
help: consider annotating `NoClone` with `#[derive(Copy)]`
28-
|
29-
LL + #[derive(Copy)]
30-
LL | struct NoClone;
31-
|
32-
33-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
3410

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

0 commit comments

Comments
 (0)