Skip to content

Commit 157bab6

Browse files
Rollup merge of #113286 - fmease:iat-dont-select-if-not-enabled, r=compiler-errors
Don't perform selection if inherent associated types are not enabled Fixes #113265. As discussed r? `@compiler-errors`
2 parents ef21fd5 + 838f85d commit 157bab6

10 files changed

+99
-46
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18931893
) -> Result<Option<(Ty<'tcx>, DefId)>, ErrorGuaranteed> {
18941894
let tcx = self.tcx();
18951895

1896+
// Don't attempt to look up inherent associated types when the feature is not enabled.
1897+
// Theoretically it'd be fine to do so since we feature-gate their definition site.
1898+
// However, due to current limitations of the implementation (caused by us performing
1899+
// selection in AstConv), IATs can lead to cycle errors (#108491, #110106) which mask the
1900+
// feature-gate error, needlessly confusing users that use IATs by accident (#113265).
1901+
if !tcx.features().inherent_associated_types {
1902+
return Ok(None);
1903+
}
1904+
18961905
let candidates: Vec<_> = tcx
18971906
.inherent_impls(adt_did)
18981907
.iter()
@@ -1903,11 +1912,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19031912
return Ok(None);
19041913
}
19051914

1906-
if !tcx.features().inherent_associated_types {
1907-
tcx.sess
1908-
.delay_span_bug(span, "found inherent assoc type without the feature being gated");
1909-
}
1910-
19111915
//
19121916
// Select applicable inherent associated type candidates modulo regions.
19131917
//
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// aux-crate:aux=assoc-inherent-unstable.rs
22
// edition: 2021
33

4+
#![feature(inherent_associated_types)]
5+
#![allow(incomplete_features)]
6+
47
type Data = aux::Owner::Data; //~ ERROR use of unstable library feature 'data'
58

69
fn main() {}

tests/ui/associated-inherent-types/assoc-inherent-unstable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: use of unstable library feature 'data'
2-
--> $DIR/assoc-inherent-unstable.rs:4:13
2+
--> $DIR/assoc-inherent-unstable.rs:7:13
33
|
44
LL | type Data = aux::Owner::Data;
55
| ^^^^^^^^^^^^^^^^

tests/ui/associated-inherent-types/bugs/cycle-iat-inside-of-adt.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// known-bug: #108491
22

3+
#![feature(inherent_associated_types)]
4+
#![allow(incomplete_features)]
35
// FIXME(inherent_associated_types): This should pass.
46

57
struct Foo {
@@ -8,3 +10,5 @@ struct Foo {
810
impl Foo {
911
pub type Bar = usize;
1012
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
1-
error[E0601]: `main` function not found in crate `cycle_iat_inside_of_adt`
2-
--> $DIR/cycle-iat-inside-of-adt.rs:10:2
3-
|
4-
LL | }
5-
| ^ consider adding a `main` function to `$DIR/cycle-iat-inside-of-adt.rs`
6-
71
error[E0391]: cycle detected when computing predicates of `Foo`
8-
--> $DIR/cycle-iat-inside-of-adt.rs:5:1
2+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
93
|
104
LL | struct Foo {
115
| ^^^^^^^^^^
126
|
137
note: ...which requires computing predicates of `Foo`...
14-
--> $DIR/cycle-iat-inside-of-adt.rs:5:1
8+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
159
|
1610
LL | struct Foo {
1711
| ^^^^^^^^^^
1812
note: ...which requires computing inferred outlives predicates of `Foo`...
19-
--> $DIR/cycle-iat-inside-of-adt.rs:5:1
13+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
2014
|
2115
LL | struct Foo {
2216
| ^^^^^^^^^^
2317
= note: ...which requires computing the inferred outlives predicates for items in this crate...
2418
note: ...which requires computing type of `Foo::bar`...
25-
--> $DIR/cycle-iat-inside-of-adt.rs:6:5
19+
--> $DIR/cycle-iat-inside-of-adt.rs:8:5
2620
|
2721
LL | bar: Self::Bar,
2822
| ^^^^^^^^^^^^^^
2923
note: ...which requires computing normalized predicates of `Foo`...
30-
--> $DIR/cycle-iat-inside-of-adt.rs:5:1
24+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
3125
|
3226
LL | struct Foo {
3327
| ^^^^^^^^^^
3428
= note: ...which again requires computing predicates of `Foo`, completing the cycle
3529
note: cycle used when collecting item types in top-level module
36-
--> $DIR/cycle-iat-inside-of-adt.rs:5:1
37-
|
38-
LL | / struct Foo {
39-
LL | | bar: Self::Bar,
40-
LL | | }
41-
LL | | impl Foo {
42-
LL | | pub type Bar = usize;
43-
LL | | }
44-
| |_^
30+
--> $DIR/cycle-iat-inside-of-adt.rs:3:1
31+
|
32+
LL | / #![feature(inherent_associated_types)]
33+
LL | | #![allow(incomplete_features)]
34+
LL | | // FIXME(inherent_associated_types): This should pass.
35+
LL | |
36+
... |
37+
LL | |
38+
LL | | fn main() {}
39+
| |____________^
4540

46-
error: aborting due to 2 previous errors
41+
error: aborting due to previous error
4742

48-
Some errors have detailed explanations: E0391, E0601.
49-
For more information about an error, try `rustc --explain E0391`.
43+
For more information about this error, try `rustc --explain E0391`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for #113265.
2+
3+
// Don't perform selection if the feature is not enabled to prevent cycle errors
4+
// that exist due to current limitations of the implementation from masking the
5+
// feature-gate error. See the aforementioned issue.
6+
// This does lead to rustc not mentioning inherent associated types at usage-sites of
7+
// IATs that were defined in an external crate but that's acceptable for now.
8+
9+
// FIXME(inherent_associated_types): Revisit this decision once the implementation is smarter.
10+
11+
// The following program would currently lead to a cycle if IATs were enabled.
12+
13+
struct S(S::P); //~ ERROR ambiguous associated type
14+
15+
impl S { type P = (); } //~ ERROR inherent associated types are unstable
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0223]: ambiguous associated type
2+
--> $DIR/dont-select-if-disabled.rs:13:10
3+
|
4+
LL | struct S(S::P);
5+
| ^^^^
6+
|
7+
help: if there were a trait named `Example` with associated type `P` implemented for `S`, you could use the fully-qualified path
8+
|
9+
LL | struct S(<S as Example>::P);
10+
| ~~~~~~~~~~~~~~~~~
11+
12+
error[E0658]: inherent associated types are unstable
13+
--> $DIR/dont-select-if-disabled.rs:15:10
14+
|
15+
LL | impl S { type P = (); }
16+
| ^^^^^^^^^^^^
17+
|
18+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
19+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors have detailed explanations: E0223, E0658.
24+
For more information about an error, try `rustc --explain E0223`.

tests/ui/associated-inherent-types/issue-109071.no_gate.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ LL | type Item = &[T];
2929
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
3030
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
3131

32-
error: aborting due to 3 previous errors
32+
error[E0223]: ambiguous associated type
33+
--> $DIR/issue-109071.rs:15:22
34+
|
35+
LL | fn T() -> Option<Self::Item> {}
36+
| ^^^^^^^^^^ help: use the fully-qualified path: `<Windows<T> as IntoIterator>::Item`
37+
38+
error: aborting due to 4 previous errors
3339

34-
Some errors have detailed explanations: E0107, E0637, E0658.
40+
Some errors have detailed explanations: E0107, E0223, E0637, E0658.
3541
For more information about an error, try `rustc --explain E0107`.

tests/ui/associated-inherent-types/issue-109071.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ impl<T> Windows { //~ ERROR: missing generics for struct `Windows`
1313

1414
impl<T> Windows<T> {
1515
fn T() -> Option<Self::Item> {}
16+
//[no_gate]~^ ERROR: ambiguous associated type
1617
}
1718

1819
fn main() {}

tests/ui/resolve/resolve-self-in-impl.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
error: `Self` is not valid in the self type of an impl block
2-
--> $DIR/resolve-self-in-impl.rs:16:6
2+
--> $DIR/resolve-self-in-impl.rs:14:13
33
|
4-
LL | impl Self {}
5-
| ^^^^
4+
LL | impl Tr for Self {}
5+
| ^^^^
66
|
77
= note: replace `Self` with a different type
88

99
error: `Self` is not valid in the self type of an impl block
10-
--> $DIR/resolve-self-in-impl.rs:17:8
10+
--> $DIR/resolve-self-in-impl.rs:15:15
1111
|
12-
LL | impl S<Self> {}
13-
| ^^^^
12+
LL | impl Tr for S<Self> {}
13+
| ^^^^
1414
|
1515
= note: replace `Self` with a different type
1616

1717
error: `Self` is not valid in the self type of an impl block
18-
--> $DIR/resolve-self-in-impl.rs:18:7
18+
--> $DIR/resolve-self-in-impl.rs:16:6
1919
|
20-
LL | impl (Self, Self) {}
21-
| ^^^^ ^^^^
20+
LL | impl Self {}
21+
| ^^^^
2222
|
2323
= note: replace `Self` with a different type
2424

2525
error: `Self` is not valid in the self type of an impl block
26-
--> $DIR/resolve-self-in-impl.rs:14:13
26+
--> $DIR/resolve-self-in-impl.rs:17:8
2727
|
28-
LL | impl Tr for Self {}
29-
| ^^^^
28+
LL | impl S<Self> {}
29+
| ^^^^
3030
|
3131
= note: replace `Self` with a different type
3232

3333
error: `Self` is not valid in the self type of an impl block
34-
--> $DIR/resolve-self-in-impl.rs:15:15
34+
--> $DIR/resolve-self-in-impl.rs:18:7
3535
|
36-
LL | impl Tr for S<Self> {}
37-
| ^^^^
36+
LL | impl (Self, Self) {}
37+
| ^^^^ ^^^^
3838
|
3939
= note: replace `Self` with a different type
4040

0 commit comments

Comments
 (0)