Skip to content

Commit 5abbb55

Browse files
Add tests and tweak error message for dyn
1 parent ba6e138 commit 5abbb55

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
151151
self.dcx()
152152
.struct_span_err(
153153
span,
154-
format!(
155-
"conflicting associated type bounds for `{item}` when \
156-
expanding trait alias"
157-
),
154+
format!("conflicting associated type bounds for `{item}`"),
158155
)
159156
.with_span_label(
160157
old_proj_span,

tests/ui/associated-type-bounds/duplicate.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ fn uncallable_rtn(_: impl Trait<foo(..): Trait<ASSOC = 3>, foo(..): Trait<ASSOC
224224

225225
fn callable_rtn(_: impl Trait<foo(..): Send, foo(..): Send, foo(..): Eq>) {}
226226

227+
type Works = dyn Iterator<Item = i32, Item = i32>;
228+
// ^~ ERROR conflicting associated type bounds
229+
230+
trait Trait2 {
231+
const ASSOC: u32;
232+
}
233+
234+
type AlsoWorks = dyn Trait2<ASSOC = 3u32, ASSOC = 3u32>;
235+
// ^~ ERROR conflicting associated type bounds
236+
227237
fn main() {
228238
callable(iter::empty::<i32>());
229239
callable_const(());

tests/ui/associated-type-bounds/duplicate_err.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ fn uncallable_const(_: impl Trait<ASSOC = 3, ASSOC = 4>) {}
7070

7171
fn uncallable_rtn(_: impl Trait<foo(..): Trait<ASSOC = 3>, foo(..): Trait<ASSOC = 4>>) {}
7272

73+
type MustFail = dyn Iterator<Item = i32, Item = u32>;
74+
//~^ ERROR conflicting associated type bounds
75+
76+
trait Trait2 {
77+
const ASSOC: u32;
78+
}
79+
80+
type MustFail2 = dyn Trait2<ASSOC = 3u32, ASSOC = 4u32>;
81+
//~^ ERROR conflicting associated type bounds
82+
7383
fn main() {
7484
uncallable(iter::empty::<u32>()); //~ ERROR [E0271]
7585
uncallable(iter::empty::<i32>()); //~ ERROR [E0271]

tests/ui/associated-type-bounds/duplicate_err.stderr

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,26 @@ LL | fn mismatch_2() -> impl Iterator<Item: Copy, Item: Send> {
9797
LL | iter::empty::<String>()
9898
| ----------------------- return type was inferred to be `std::iter::Empty<String>` here
9999

100+
error: conflicting associated type bounds for `Item`
101+
--> $DIR/duplicate_err.rs:73:17
102+
|
103+
LL | type MustFail = dyn Iterator<Item = i32, Item = u32>;
104+
| ^^^^^^^^^^^^^----------^^----------^
105+
| | |
106+
| | `Item` is specified to be `u32` here
107+
| `Item` is specified to be `i32` here
108+
109+
error: conflicting associated type bounds for `ASSOC`
110+
--> $DIR/duplicate_err.rs:80:18
111+
|
112+
LL | type MustFail2 = dyn Trait2<ASSOC = 3u32, ASSOC = 4u32>;
113+
| ^^^^^^^^^^^------------^^------------^
114+
| | |
115+
| | `ASSOC` is specified to be `4` here
116+
| `ASSOC` is specified to be `3` here
117+
100118
error[E0271]: expected `Empty<u32>` to be an iterator that yields `i32`, but it yields `u32`
101-
--> $DIR/duplicate_err.rs:74:16
119+
--> $DIR/duplicate_err.rs:84:16
102120
|
103121
LL | uncallable(iter::empty::<u32>());
104122
| ---------- ^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
@@ -112,7 +130,7 @@ LL | fn uncallable(_: impl Iterator<Item = i32, Item = u32>) {}
112130
| ^^^^^^^^^^ required by this bound in `uncallable`
113131

114132
error[E0271]: expected `Empty<i32>` to be an iterator that yields `u32`, but it yields `i32`
115-
--> $DIR/duplicate_err.rs:75:16
133+
--> $DIR/duplicate_err.rs:85:16
116134
|
117135
LL | uncallable(iter::empty::<i32>());
118136
| ---------- ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `i32`
@@ -126,7 +144,7 @@ LL | fn uncallable(_: impl Iterator<Item = i32, Item = u32>) {}
126144
| ^^^^^^^^^^ required by this bound in `uncallable`
127145

128146
error[E0271]: type mismatch resolving `<() as Trait>::ASSOC == 4`
129-
--> $DIR/duplicate_err.rs:76:22
147+
--> $DIR/duplicate_err.rs:86:22
130148
|
131149
LL | uncallable_const(());
132150
| ---------------- ^^ expected `4`, found `3`
@@ -142,7 +160,7 @@ LL | fn uncallable_const(_: impl Trait<ASSOC = 3, ASSOC = 4>) {}
142160
| ^^^^^^^^^ required by this bound in `uncallable_const`
143161

144162
error[E0271]: type mismatch resolving `<u32 as Trait>::ASSOC == 3`
145-
--> $DIR/duplicate_err.rs:77:22
163+
--> $DIR/duplicate_err.rs:87:22
146164
|
147165
LL | uncallable_const(4u32);
148166
| ---------------- ^^^^ expected `3`, found `4`
@@ -158,7 +176,7 @@ LL | fn uncallable_const(_: impl Trait<ASSOC = 3, ASSOC = 4>) {}
158176
| ^^^^^^^^^ required by this bound in `uncallable_const`
159177

160178
error[E0271]: type mismatch resolving `<() as Trait>::ASSOC == 4`
161-
--> $DIR/duplicate_err.rs:78:20
179+
--> $DIR/duplicate_err.rs:88:20
162180
|
163181
LL | uncallable_rtn(());
164182
| -------------- ^^ expected `4`, found `3`
@@ -174,7 +192,7 @@ LL | fn uncallable_rtn(_: impl Trait<foo(..): Trait<ASSOC = 3>, foo(..): Trait<A
174192
| ^^^^^^^^^ required by this bound in `uncallable_rtn`
175193

176194
error[E0271]: type mismatch resolving `<u32 as Trait>::ASSOC == 3`
177-
--> $DIR/duplicate_err.rs:79:20
195+
--> $DIR/duplicate_err.rs:89:20
178196
|
179197
LL | uncallable_rtn(17u32);
180198
| -------------- ^^^^^ expected `3`, found `4`
@@ -189,7 +207,7 @@ note: required by a bound in `uncallable_rtn`
189207
LL | fn uncallable_rtn(_: impl Trait<foo(..): Trait<ASSOC = 3>, foo(..): Trait<ASSOC = 4>>) {}
190208
| ^^^^^^^^^ required by this bound in `uncallable_rtn`
191209

192-
error: aborting due to 17 previous errors
210+
error: aborting due to 19 previous errors
193211

194212
Some errors have detailed explanations: E0271, E0277, E0282.
195213
For more information about an error, try `rustc --explain E0271`.

tests/ui/associated-types/associated-types-overridden-binding-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: conflicting associated type bounds for `Item` when expanding trait alias
1+
error: conflicting associated type bounds for `Item`
22
--> $DIR/associated-types-overridden-binding-2.rs:6:13
33
|
44
LL | trait I32Iterator = Iterator<Item = i32>;

tests/ui/associated-types/associated-types-overridden-binding.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ note: required by a bound in `I32Iterator`
2222
LL | trait I32Iterator = Iterator<Item = i32>;
2323
| ^^^^^^^^^^ required by this bound in `I32Iterator`
2424

25-
error: conflicting associated type bounds for `Item` when expanding trait alias
25+
error: conflicting associated type bounds for `Item`
2626
--> $DIR/associated-types-overridden-binding.rs:10:13
2727
|
2828
LL | trait I32Iterator = Iterator<Item = i32>;

0 commit comments

Comments
 (0)