Skip to content

Commit 3b2bbcd

Browse files
committed
internal constraints are better than placeholder outlives
1 parent 4919d55 commit 3b2bbcd

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,9 +1726,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17261726
// `BoringNoLocation` constraints can point to user-written code, but are less
17271727
// specific, and are not used for relations that would make sense to blame.
17281728
ConstraintCategory::BoringNoLocation => 6,
1729-
// Do not blame internal constraints.
1730-
ConstraintCategory::OutlivesUnnameablePlaceholder(_) => 7,
1731-
ConstraintCategory::Internal => 8,
1729+
// Do not blame internal constraints if we can avoid it. Never blame
1730+
// the `'region: 'static` constraints introduced by placeholder outlives.
1731+
ConstraintCategory::Internal => 7,
1732+
ConstraintCategory::OutlivesUnnameablePlaceholder(_) => 8,
17321733
};
17331734

17341735
debug!("constraint {constraint:?} category: {category:?}, interest: {interest:?}");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ compile-flags: -Zdeduplicate-diagnostics=yes
2+
3+
// Regression test for #146467.
4+
#![feature(inherent_associated_types)]
5+
//~^ WARN the feature `inherent_associated_types` is incomplete
6+
7+
struct Foo<T>(T);
8+
9+
impl<'a> Foo<fn(&())> {
10+
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait
11+
type Assoc = &'a ();
12+
}
13+
14+
fn foo(_: for<'a> fn(Foo<fn(&'a ())>::Assoc)) {}
15+
//~^ ERROR mismatched types
16+
//~| ERROR higher-ranked subtype error
17+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
warning: the feature `inherent_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/hr-do-not-blame-outlives-static-ice.rs:4:12
3+
|
4+
LL | #![feature(inherent_associated_types)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
11+
--> $DIR/hr-do-not-blame-outlives-static-ice.rs:9:6
12+
|
13+
LL | impl<'a> Foo<fn(&())> {
14+
| ^^ unconstrained lifetime parameter
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/hr-do-not-blame-outlives-static-ice.rs:14:11
18+
|
19+
LL | fn foo(_: for<'a> fn(Foo<fn(&'a ())>::Assoc)) {}
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
21+
|
22+
= note: expected struct `Foo<for<'a> fn(&'a ())>`
23+
found struct `Foo<for<'a> fn(&'a ())>`
24+
25+
error: higher-ranked subtype error
26+
--> $DIR/hr-do-not-blame-outlives-static-ice.rs:14:1
27+
|
28+
LL | fn foo(_: for<'a> fn(Foo<fn(&'a ())>::Assoc)) {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 3 previous errors; 1 warning emitted
32+
33+
Some errors have detailed explanations: E0207, E0308.
34+
For more information about an error, try `rustc --explain E0207`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: -Zdeduplicate-diagnostics=yes
2+
3+
// Regression test for #146467.
4+
trait Trait { type Assoc; }
5+
6+
impl Trait for fn(&()) { type Assoc = (); }
7+
8+
fn f(_: for<'a> fn(<fn(&'a ()) as Trait>::Assoc)) {}
9+
//~^ ERROR implementation of `Trait` is not general enough
10+
//~| ERROR higher-ranked subtype error
11+
12+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: implementation of `Trait` is not general enough
2+
--> $DIR/do-not-blame-outlives-static-ice.rs:8:9
3+
|
4+
LL | fn f(_: for<'a> fn(<fn(&'a ()) as Trait>::Assoc)) {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Trait` is not general enough
6+
|
7+
= note: `for<'a> fn(&'a ())` must implement `Trait`, for any lifetime `'0`...
8+
= note: ...but `Trait` is actually implemented for the type `for<'a> fn(&'a ())`
9+
10+
error: higher-ranked subtype error
11+
--> $DIR/do-not-blame-outlives-static-ice.rs:8:1
12+
|
13+
LL | fn f(_: for<'a> fn(<fn(&'a ()) as Trait>::Assoc)) {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)