Skip to content

Commit 73c79cd

Browse files
committed
stop special-casing 'static in evaluate
1 parent 9bdb488 commit 73c79cd

File tree

7 files changed

+151
-102
lines changed

7 files changed

+151
-102
lines changed

compiler/rustc_infer/src/infer/freshen.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,16 @@ pub struct TypeFreshener<'a, 'tcx> {
4343
const_freshen_count: u32,
4444
ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
4545
const_freshen_map: FxHashMap<ty::InferConst<'tcx>, ty::Const<'tcx>>,
46-
keep_static: bool,
4746
}
4847

4948
impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
50-
pub fn new(infcx: &'a InferCtxt<'tcx>, keep_static: bool) -> TypeFreshener<'a, 'tcx> {
49+
pub fn new(infcx: &'a InferCtxt<'tcx>) -> TypeFreshener<'a, 'tcx> {
5150
TypeFreshener {
5251
infcx,
5352
ty_freshen_count: 0,
5453
const_freshen_count: 0,
5554
ty_freshen_map: Default::default(),
5655
const_freshen_map: Default::default(),
57-
keep_static,
5856
}
5957
}
6058

@@ -121,18 +119,9 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
121119
| ty::ReFree(_)
122120
| ty::ReVar(_)
123121
| ty::RePlaceholder(..)
122+
| ty::ReStatic
124123
| ty::ReError(_)
125-
| ty::ReErased => {
126-
// replace all free regions with 'erased
127-
self.interner().lifetimes.re_erased
128-
}
129-
ty::ReStatic => {
130-
if self.keep_static {
131-
r
132-
} else {
133-
self.interner().lifetimes.re_erased
134-
}
135-
}
124+
| ty::ReErased => self.interner().lifetimes.re_erased,
136125
}
137126
}
138127

compiler/rustc_infer/src/infer/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,7 @@ impl<'tcx> InferCtxt<'tcx> {
706706
}
707707

708708
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
709-
freshen::TypeFreshener::new(self, false)
710-
}
711-
712-
/// Like `freshener`, but does not replace `'static` regions.
713-
pub fn freshener_keep_static<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
714-
freshen::TypeFreshener::new(self, true)
709+
freshen::TypeFreshener::new(self)
715710
}
716711

717712
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {

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

+86-78
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
211211
pub fn new(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
212212
SelectionContext {
213213
infcx,
214-
freshener: infcx.freshener_keep_static(),
214+
freshener: infcx.freshener(),
215215
intercrate_ambiguity_causes: None,
216216
query_mode: TraitQueryMode::Standard,
217217
}
@@ -769,14 +769,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
769769
}
770770

771771
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(pred)) => {
772-
// A global type with no late-bound regions can only
773-
// contain the "'static" lifetime (any other lifetime
774-
// would either be late-bound or local), so it is guaranteed
775-
// to outlive any other lifetime
776-
if pred.0.is_global() && !pred.0.has_late_bound_vars() {
777-
Ok(EvaluatedToOk)
778-
} else {
772+
// A global type with no free lifetimes or generic parameters
773+
// outlives anything.
774+
if pred.0.has_free_regions()
775+
|| pred.0.has_late_bound_regions()
776+
|| pred.0.has_non_region_infer()
777+
|| pred.0.has_non_region_infer()
778+
{
779779
Ok(EvaluatedToOkModuloRegions)
780+
} else {
781+
Ok(EvaluatedToOk)
780782
}
781783
}
782784

@@ -1824,6 +1826,12 @@ enum DropVictim {
18241826
No,
18251827
}
18261828

1829+
impl DropVictim {
1830+
fn drop_if(should_drop: bool) -> DropVictim {
1831+
if should_drop { DropVictim::Yes } else { DropVictim::No }
1832+
}
1833+
}
1834+
18271835
/// ## Winnowing
18281836
///
18291837
/// Winnowing is the process of attempting to resolve ambiguity by
@@ -1889,11 +1897,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18891897
// or the current one if tied (they should both evaluate to the same answer). This is
18901898
// probably best characterized as a "hack", since we might prefer to just do our
18911899
// best to *not* create essentially duplicate candidates in the first place.
1892-
if other.bound_vars().len() <= victim.bound_vars().len() {
1893-
DropVictim::Yes
1894-
} else {
1895-
DropVictim::No
1896-
}
1900+
DropVictim::drop_if(other.bound_vars().len() <= victim.bound_vars().len())
18971901
} else if other.skip_binder().trait_ref == victim.skip_binder().trait_ref
18981902
&& victim.skip_binder().constness == ty::BoundConstness::NotConst
18991903
&& other.skip_binder().polarity == victim.skip_binder().polarity
@@ -1923,17 +1927,13 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19231927
| ObjectCandidate(_)
19241928
| ProjectionCandidate(..),
19251929
) => {
1926-
if is_global(other_cand) {
1927-
DropVictim::No
1928-
} else {
1929-
// We have a where clause so don't go around looking
1930-
// for impls. Arbitrarily give param candidates priority
1931-
// over projection and object candidates.
1932-
//
1933-
// Global bounds from the where clause should be ignored
1934-
// here (see issue #50825).
1935-
DropVictim::Yes
1936-
}
1930+
// We have a where clause so don't go around looking
1931+
// for impls. Arbitrarily give param candidates priority
1932+
// over projection and object candidates.
1933+
//
1934+
// Global bounds from the where clause should be ignored
1935+
// here (see issue #50825).
1936+
DropVictim::drop_if(!is_global(other_cand))
19371937
}
19381938
(ObjectCandidate(_) | ProjectionCandidate(..), ParamCandidate(ref victim_cand)) => {
19391939
// Prefer these to a global where-clause bound
@@ -1955,18 +1955,16 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19551955
) => {
19561956
// Prefer these to a global where-clause bound
19571957
// (see issue #50825).
1958-
if is_global(victim_cand) && other.evaluation.must_apply_modulo_regions() {
1959-
DropVictim::Yes
1960-
} else {
1961-
DropVictim::No
1962-
}
1958+
DropVictim::drop_if(
1959+
is_global(victim_cand) && other.evaluation.must_apply_modulo_regions(),
1960+
)
19631961
}
19641962

19651963
(ProjectionCandidate(i, _), ProjectionCandidate(j, _))
19661964
| (ObjectCandidate(i), ObjectCandidate(j)) => {
19671965
// Arbitrarily pick the lower numbered candidate for backwards
19681966
// compatibility reasons. Don't let this affect inference.
1969-
if i < j && !needs_infer { DropVictim::Yes } else { DropVictim::No }
1967+
DropVictim::drop_if(i < j && !needs_infer)
19701968
}
19711969
(ObjectCandidate(_), ProjectionCandidate(..))
19721970
| (ProjectionCandidate(..), ObjectCandidate(_)) => {
@@ -2017,55 +2015,65 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20172015
}
20182016
}
20192017

2020-
if other.evaluation.must_apply_considering_regions() {
2021-
match tcx.impls_are_allowed_to_overlap(other_def, victim_def) {
2022-
Some(ty::ImplOverlapKind::Permitted { marker: true }) => {
2023-
// Subtle: If the predicate we are evaluating has inference
2024-
// variables, do *not* allow discarding candidates due to
2025-
// marker trait impls.
2026-
//
2027-
// Without this restriction, we could end up accidentally
2028-
// constraining inference variables based on an arbitrarily
2029-
// chosen trait impl.
2030-
//
2031-
// Imagine we have the following code:
2032-
//
2033-
// ```rust
2034-
// #[marker] trait MyTrait {}
2035-
// impl MyTrait for u8 {}
2036-
// impl MyTrait for bool {}
2037-
// ```
2038-
//
2039-
// And we are evaluating the predicate `<_#0t as MyTrait>`.
2040-
//
2041-
// During selection, we will end up with one candidate for each
2042-
// impl of `MyTrait`. If we were to discard one impl in favor
2043-
// of the other, we would be left with one candidate, causing
2044-
// us to "successfully" select the predicate, unifying
2045-
// _#0t with (for example) `u8`.
2046-
//
2047-
// However, we have no reason to believe that this unification
2048-
// is correct - we've essentially just picked an arbitrary
2049-
// *possibility* for _#0t, and required that this be the *only*
2050-
// possibility.
2051-
//
2052-
// Eventually, we will either:
2053-
// 1) Unify all inference variables in the predicate through
2054-
// some other means (e.g. type-checking of a function). We will
2055-
// then be in a position to drop marker trait candidates
2056-
// without constraining inference variables (since there are
2057-
// none left to constrain)
2058-
// 2) Be left with some unconstrained inference variables. We
2059-
// will then correctly report an inference error, since the
2060-
// existence of multiple marker trait impls tells us nothing
2061-
// about which one should actually apply.
2062-
if needs_infer { DropVictim::No } else { DropVictim::Yes }
2063-
}
2064-
Some(_) => DropVictim::Yes,
2065-
None => DropVictim::No,
2018+
match tcx.impls_are_allowed_to_overlap(other_def, victim_def) {
2019+
// For #33140 the impl headers must be exactly equal, the trait must not have
2020+
// any associated items and there are no where-clauses.
2021+
//
2022+
// We can just arbitrarily drop one of the impls.
2023+
Some(ty::ImplOverlapKind::Issue33140) => {
2024+
assert_eq!(other.evaluation, victim.evaluation);
2025+
DropVictim::Yes
20662026
}
2067-
} else {
2068-
DropVictim::No
2027+
// For candidates which already reference errors it doesn't really
2028+
// matter what we do 🤷
2029+
Some(ty::ImplOverlapKind::Permitted { marker: false }) => {
2030+
DropVictim::drop_if(other.evaluation.must_apply_considering_regions())
2031+
}
2032+
Some(ty::ImplOverlapKind::Permitted { marker: true }) => {
2033+
// Subtle: If the predicate we are evaluating has inference
2034+
// variables, do *not* allow discarding candidates due to
2035+
// marker trait impls.
2036+
//
2037+
// Without this restriction, we could end up accidentally
2038+
// constraining inference variables based on an arbitrarily
2039+
// chosen trait impl.
2040+
//
2041+
// Imagine we have the following code:
2042+
//
2043+
// ```rust
2044+
// #[marker] trait MyTrait {}
2045+
// impl MyTrait for u8 {}
2046+
// impl MyTrait for bool {}
2047+
// ```
2048+
//
2049+
// And we are evaluating the predicate `<_#0t as MyTrait>`.
2050+
//
2051+
// During selection, we will end up with one candidate for each
2052+
// impl of `MyTrait`. If we were to discard one impl in favor
2053+
// of the other, we would be left with one candidate, causing
2054+
// us to "successfully" select the predicate, unifying
2055+
// _#0t with (for example) `u8`.
2056+
//
2057+
// However, we have no reason to believe that this unification
2058+
// is correct - we've essentially just picked an arbitrary
2059+
// *possibility* for _#0t, and required that this be the *only*
2060+
// possibility.
2061+
//
2062+
// Eventually, we will either:
2063+
// 1) Unify all inference variables in the predicate through
2064+
// some other means (e.g. type-checking of a function). We will
2065+
// then be in a position to drop marker trait candidates
2066+
// without constraining inference variables (since there are
2067+
// none left to constrain)
2068+
// 2) Be left with some unconstrained inference variables. We
2069+
// will then correctly report an inference error, since the
2070+
// existence of multiple marker trait impls tells us nothing
2071+
// about which one should actually apply.
2072+
DropVictim::drop_if(
2073+
!needs_infer && other.evaluation.must_apply_considering_regions(),
2074+
)
2075+
}
2076+
None => DropVictim::No,
20692077
}
20702078
}
20712079

tests/ui/marker_trait_attr/overlap-marker-trait-with-static-lifetime.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// check-pass
1+
// known-bug: #89515
2+
//
3+
// The trait solver cannot deal with ambiguous marker trait impls
4+
// if there are lifetimes involved. As we must not special-case any
5+
// regions this does not work, even with 'static
26
#![feature(marker_trait_attr)]
37

48
#[marker]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0283]: type annotations needed: cannot satisfy `&'static (): Marker`
2+
--> $DIR/overlap-marker-trait-with-static-lifetime.rs:11:17
3+
|
4+
LL | impl Marker for &'static () {}
5+
| ^^^^^^^^^^^
6+
|
7+
note: multiple `impl`s satisfying `&'static (): Marker` found
8+
--> $DIR/overlap-marker-trait-with-static-lifetime.rs:11:1
9+
|
10+
LL | impl Marker for &'static () {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | impl Marker for &'static () {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error[E0283]: type annotations needed: cannot satisfy `&'static (): Marker`
16+
--> $DIR/overlap-marker-trait-with-static-lifetime.rs:12:17
17+
|
18+
LL | impl Marker for &'static () {}
19+
| ^^^^^^^^^^^
20+
|
21+
note: multiple `impl`s satisfying `&'static (): Marker` found
22+
--> $DIR/overlap-marker-trait-with-static-lifetime.rs:11:1
23+
|
24+
LL | impl Marker for &'static () {}
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
LL | impl Marker for &'static () {}
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0283`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
// check-pass
1+
// known-bug: #109481
2+
//
3+
// While the `T: Copy` is always applicable when checking
4+
// that the impl `impl<T: Copy> F for T {}` is well formed,
5+
// the old trait solver can only approximate this by checking
6+
// that there are no inference variables in the obligation and
7+
// no region constraints in the evaluation result.
8+
//
9+
// Because of this we end up with ambiguity here.
210
#![feature(marker_trait_attr)]
311

412
#[marker]
513
pub trait F {}
6-
impl<T> F for T where T: Copy {}
7-
impl<T> F for T where T: 'static {}
14+
impl<T: Copy> F for T {}
15+
impl<T: 'static> F for T {}
816

917
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0310]: the parameter type `T` may not live long enough
2+
--> $DIR/overlapping-impl-1-modulo-regions.rs:14:21
3+
|
4+
LL | impl<T: Copy> F for T {}
5+
| ^ ...so that the type `T` will meet its required lifetime bounds
6+
|
7+
help: consider adding an explicit lifetime bound...
8+
|
9+
LL | impl<T: Copy + 'static> F for T {}
10+
| +++++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)