Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit daf0ad6

Browse files
authoredMay 21, 2020
Rollup merge of rust-lang#72149 - estebank:icemation, r=eddyb
Don't `type_of` on trait assoc ty without default Fix rust-lang#72076.
2 parents 148c125 + a3f30bb commit daf0ad6

5 files changed

+54
-17
lines changed
 

‎src/librustc_middle/ty/error.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -815,19 +815,18 @@ fn foo(&self) -> Self::T { String::new() }
815815
for item in &items[..] {
816816
match item.kind {
817817
hir::AssocItemKind::Type | hir::AssocItemKind::OpaqueTy => {
818-
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
819-
if let hir::Defaultness::Default { has_value: true } =
820-
item.defaultness
821-
{
818+
// FIXME: account for returning some type in a trait fn impl that has
819+
// an assoc type as a return type (#72076).
820+
if let hir::Defaultness::Default { has_value: true } = item.defaultness
821+
{
822+
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
822823
db.span_label(
823824
item.span,
824825
"associated type defaults can't be assumed inside the \
825826
trait defining them",
826827
);
827-
} else {
828-
db.span_label(item.span, "expected this associated type");
828+
return true;
829829
}
830-
return true;
831830
}
832831
}
833832
_ => {}

‎src/test/ui/issues/issue-72076.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait X {
2+
type S;
3+
fn f() -> Self::S {} //~ ERROR mismatched types
4+
}
5+
6+
fn main() {}

‎src/test/ui/issues/issue-72076.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-72076.rs:3:23
3+
|
4+
LL | fn f() -> Self::S {}
5+
| ^^ expected associated type, found `()`
6+
|
7+
= note: expected associated type `<Self as X>::S`
8+
found unit type `()`
9+
= help: consider constraining the associated type `<Self as X>::S` to `()` or calling a method that returns `<Self as X>::S`
10+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

‎src/test/ui/suggestions/trait-with-missing-associated-type-restriction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ trait Trait<T = Self> {
77

88
fn func(&self) -> Self::A;
99
fn funk(&self, _: Self::A);
10+
fn funq(&self) -> Self::A {} //~ ERROR mismatched types
1011
}
1112

1213
fn foo(_: impl Trait, x: impl Trait) {

‎src/test/ui/suggestions/trait-with-missing-associated-type-restriction.stderr

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
error[E0308]: mismatched types
2-
--> $DIR/trait-with-missing-associated-type-restriction.rs:13:9
2+
--> $DIR/trait-with-missing-associated-type-restriction.rs:10:31
3+
|
4+
LL | fn funq(&self) -> Self::A {}
5+
| ^^ expected associated type, found `()`
6+
|
7+
= note: expected associated type `<Self as Trait<T>>::A`
8+
found unit type `()`
9+
help: a method is available that returns `<Self as Trait<T>>::A`
10+
--> $DIR/trait-with-missing-associated-type-restriction.rs:8:5
11+
|
12+
LL | fn func(&self) -> Self::A;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::func`
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/trait-with-missing-associated-type-restriction.rs:14:9
317
|
418
LL | qux(x.func())
519
| ^^^^^^^^ expected `usize`, found associated type
@@ -12,7 +26,7 @@ LL | fn foo(_: impl Trait, x: impl Trait<A = usize>) {
1226
| ^^^^^^^^^^^
1327

1428
error[E0308]: mismatched types
15-
--> $DIR/trait-with-missing-associated-type-restriction.rs:17:9
29+
--> $DIR/trait-with-missing-associated-type-restriction.rs:18:9
1630
|
1731
LL | qux(x.func())
1832
| ^^^^^^^^ expected `usize`, found associated type
@@ -25,7 +39,7 @@ LL | fn bar<T: Trait<A = usize>>(x: T) {
2539
| ^^^^^^^^^^^
2640

2741
error[E0308]: mismatched types
28-
--> $DIR/trait-with-missing-associated-type-restriction.rs:21:9
42+
--> $DIR/trait-with-missing-associated-type-restriction.rs:22:9
2943
|
3044
LL | qux(x.func())
3145
| ^^^^^^^^ expected `usize`, found associated type
@@ -38,25 +52,28 @@ LL | fn foo2(x: impl Trait<i32, A = usize>) {
3852
| ^^^^^^^^^^^
3953

4054
error[E0308]: mismatched types
41-
--> $DIR/trait-with-missing-associated-type-restriction.rs:25:12
55+
--> $DIR/trait-with-missing-associated-type-restriction.rs:26:12
4256
|
4357
LL | x.funk(3);
4458
| ^ expected associated type, found integer
4559
|
4660
= note: expected associated type `<T as Trait<i32>>::A`
4761
found type `{integer}`
48-
help: a method is available that returns `<T as Trait<i32>>::A`
62+
help: some methods are available that return `<T as Trait<i32>>::A`
4963
--> $DIR/trait-with-missing-associated-type-restriction.rs:8:5
5064
|
5165
LL | fn func(&self) -> Self::A;
5266
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::func`
67+
LL | fn funk(&self, _: Self::A);
68+
LL | fn funq(&self) -> Self::A {}
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::funq`
5370
help: consider constraining the associated type `<T as Trait<i32>>::A` to `{integer}`
5471
|
5572
LL | fn bar2<T: Trait<i32, A = {integer}>>(x: T) {
5673
| ^^^^^^^^^^^^^^^
5774

5875
error[E0308]: mismatched types
59-
--> $DIR/trait-with-missing-associated-type-restriction.rs:26:9
76+
--> $DIR/trait-with-missing-associated-type-restriction.rs:27:9
6077
|
6178
LL | qux(x.func())
6279
| ^^^^^^^^ expected `usize`, found associated type
@@ -69,7 +86,7 @@ LL | fn bar2<T: Trait<i32, A = usize>>(x: T) {
6986
| ^^^^^^^^^^^
7087

7188
error[E0308]: mismatched types
72-
--> $DIR/trait-with-missing-associated-type-restriction.rs:30:9
89+
--> $DIR/trait-with-missing-associated-type-restriction.rs:31:9
7390
|
7491
LL | fn baz<D: std::fmt::Debug, T: Trait<A = D>>(x: T) {
7592
| - this type parameter
@@ -80,13 +97,13 @@ LL | qux(x.func())
8097
found type parameter `D`
8198

8299
error[E0308]: mismatched types
83-
--> $DIR/trait-with-missing-associated-type-restriction.rs:34:9
100+
--> $DIR/trait-with-missing-associated-type-restriction.rs:35:9
84101
|
85102
LL | qux(x.func())
86103
| ^^^^^^^^ expected `usize`, found `()`
87104

88105
error[E0308]: mismatched types
89-
--> $DIR/trait-with-missing-associated-type-restriction.rs:38:9
106+
--> $DIR/trait-with-missing-associated-type-restriction.rs:39:9
90107
|
91108
LL | qux(x.func())
92109
| ^^^^^^^^ expected `usize`, found associated type
@@ -98,6 +115,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
98115
LL | fn ban<T>(x: T) where T: Trait<A = usize> {
99116
| ^^^^^^^^^^^
100117

101-
error: aborting due to 8 previous errors
118+
error: aborting due to 9 previous errors
102119

103120
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)
Please sign in to comment.