Skip to content

Commit d531a84

Browse files
committed
trait_sel: skip nominal_obligations for Sized
`nominal_obligations` calls `predicates_of` on a `Sized` obligation, effectively elaborating the trait and making the well-formedness checking machinery do a bunch of extra work checking a `MetaSized` obligation is well-formed, but given that both `Sized` and `MetaSized` are built-ins, if `Sized` is otherwise well-formed, so `MetaSized` will be.
1 parent 607eb32 commit d531a84

File tree

4 files changed

+12
-36
lines changed

4 files changed

+12
-36
lines changed

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
567567
def_id: DefId,
568568
args: GenericArgsRef<'tcx>,
569569
) -> PredicateObligations<'tcx> {
570+
// PERF: `Sized`'s predicates include `MetaSized`, but both are compiler implemented marker
571+
// traits, so `MetaSized` will always be WF if `Sized` is WF and vice-versa. Determining
572+
// the nominal obligations of `Sized` would in-effect just elaborate `MetaSized` and make
573+
// the compiler do a bunch of work needlessly.
574+
if self.tcx().is_lang_item(def_id, LangItem::Sized) {
575+
return Default::default();
576+
}
577+
570578
let predicates = self.tcx().predicates_of(def_id);
571579
let mut origins = vec![def_id; predicates.predicates.len()];
572580
let mut head = predicates;
Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:13:23
3-
|
4-
LL | (MyType<'a, T>,): Sized,
5-
| ^^^^^ lifetime mismatch
6-
|
7-
= note: expected trait `<MyType<'a, T> as Sized>`
8-
found trait `<MyType<'static, T> as Sized>`
9-
note: the lifetime `'a` as defined here...
10-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:11:8
11-
|
12-
LL | fn foo<'a, T: ?Sized>()
13-
| ^^
14-
= note: ...does not necessarily outlive the static lifetime
15-
161
error: lifetime may not live long enough
17-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:22:5
2+
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:20:5
183
|
194
LL | fn foo<'a, T: ?Sized>()
205
| -- lifetime `'a` defined here
216
...
227
LL | is_sized::<(MyType<'a, T>,)>();
238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
249

25-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2611

27-
For more information about this error, try `rustc --explain E0308`.
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
error[E0478]: lifetime bound not satisfied
2-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:13:23
3-
|
4-
LL | (MyType<'a, T>,): Sized,
5-
| ^^^^^
6-
|
7-
note: lifetime parameter instantiated with the lifetime `'a` as defined here
8-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:11:8
9-
|
10-
LL | fn foo<'a, T: ?Sized>()
11-
| ^^
12-
= note: but lifetime parameter must outlive the static lifetime
13-
141
error: lifetime may not live long enough
15-
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:22:5
2+
--> $DIR/lifetime-incomplete-prefer-sized-builtin-over-wc.rs:20:5
163
|
174
LL | fn foo<'a, T: ?Sized>()
185
| -- lifetime `'a` defined here
196
...
207
LL | is_sized::<(MyType<'a, T>,)>();
218
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
229

23-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2411

25-
For more information about this error, try `rustc --explain E0478`.

tests/ui/traits/lifetime-incomplete-prefer-sized-builtin-over-wc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ fn is_sized<T>() {}
1111
fn foo<'a, T: ?Sized>()
1212
where
1313
(MyType<'a, T>,): Sized,
14-
//[current]~^ ERROR mismatched types
15-
//[next]~^^ ERROR lifetime bound not satisfied
1614
MyType<'static, T>: Sized,
1715
{
1816
// Preferring the builtin `Sized` impl of tuples

0 commit comments

Comments
 (0)