parent
d903a9def4
commit
a3f30bbc2d
5 changed files with 54 additions and 17 deletions
|
|
@ -817,19 +817,18 @@ fn foo(&self) -> Self::T { String::new() }
|
|||
for item in &items[..] {
|
||||
match item.kind {
|
||||
hir::AssocItemKind::Type | hir::AssocItemKind::OpaqueTy => {
|
||||
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
|
||||
if let hir::Defaultness::Default { has_value: true } =
|
||||
item.defaultness
|
||||
{
|
||||
// FIXME: account for returning some type in a trait fn impl that has
|
||||
// an assoc type as a return type (#72076).
|
||||
if let hir::Defaultness::Default { has_value: true } = item.defaultness
|
||||
{
|
||||
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
|
||||
db.span_label(
|
||||
item.span,
|
||||
"associated type defaults can't be assumed inside the \
|
||||
trait defining them",
|
||||
);
|
||||
} else {
|
||||
db.span_label(item.span, "expected this associated type");
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
|||
6
src/test/ui/issues/issue-72076.rs
Normal file
6
src/test/ui/issues/issue-72076.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
trait X {
|
||||
type S;
|
||||
fn f() -> Self::S {} //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
14
src/test/ui/issues/issue-72076.stderr
Normal file
14
src/test/ui/issues/issue-72076.stderr
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/issue-72076.rs:3:23
|
||||
|
|
||||
LL | fn f() -> Self::S {}
|
||||
| ^^ expected associated type, found `()`
|
||||
|
|
||||
= note: expected associated type `<Self as X>::S`
|
||||
found unit type `()`
|
||||
= help: consider constraining the associated type `<Self as X>::S` to `()` or calling a method that returns `<Self as X>::S`
|
||||
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
|
|
@ -7,6 +7,7 @@ trait Trait<T = Self> {
|
|||
|
||||
fn func(&self) -> Self::A;
|
||||
fn funk(&self, _: Self::A);
|
||||
fn funq(&self) -> Self::A {} //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn foo(_: impl Trait, x: impl Trait) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,19 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:13:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:10:31
|
||||
|
|
||||
LL | fn funq(&self) -> Self::A {}
|
||||
| ^^ expected associated type, found `()`
|
||||
|
|
||||
= note: expected associated type `<Self as Trait<T>>::A`
|
||||
found unit type `()`
|
||||
help: a method is available that returns `<Self as Trait<T>>::A`
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:8:5
|
||||
|
|
||||
LL | fn func(&self) -> Self::A;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::func`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:14:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found associated type
|
||||
|
|
@ -12,7 +26,7 @@ LL | fn foo(_: impl Trait, x: impl Trait<A = usize>) {
|
|||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:17:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:18:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found associated type
|
||||
|
|
@ -25,7 +39,7 @@ LL | fn bar<T: Trait<A = usize>>(x: T) {
|
|||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:21:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:22:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found associated type
|
||||
|
|
@ -38,25 +52,28 @@ LL | fn foo2(x: impl Trait<i32, A = usize>) {
|
|||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:25:12
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:26:12
|
||||
|
|
||||
LL | x.funk(3);
|
||||
| ^ expected associated type, found integer
|
||||
|
|
||||
= note: expected associated type `<T as Trait<i32>>::A`
|
||||
found type `{integer}`
|
||||
help: a method is available that returns `<T as Trait<i32>>::A`
|
||||
help: some methods are available that return `<T as Trait<i32>>::A`
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:8:5
|
||||
|
|
||||
LL | fn func(&self) -> Self::A;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::func`
|
||||
LL | fn funk(&self, _: Self::A);
|
||||
LL | fn funq(&self) -> Self::A {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling `Trait::funq`
|
||||
help: consider constraining the associated type `<T as Trait<i32>>::A` to `{integer}`
|
||||
|
|
||||
LL | fn bar2<T: Trait<i32, A = {integer}>>(x: T) {
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:26:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:27:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found associated type
|
||||
|
|
@ -69,7 +86,7 @@ LL | fn bar2<T: Trait<i32, A = usize>>(x: T) {
|
|||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:30:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:31:9
|
||||
|
|
||||
LL | fn baz<D: std::fmt::Debug, T: Trait<A = D>>(x: T) {
|
||||
| - this type parameter
|
||||
|
|
@ -80,13 +97,13 @@ LL | qux(x.func())
|
|||
found type parameter `D`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:34:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:35:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found `()`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:38:9
|
||||
--> $DIR/trait-with-missing-associated-type-restriction.rs:39:9
|
||||
|
|
||||
LL | qux(x.func())
|
||||
| ^^^^^^^^ expected `usize`, found associated type
|
||||
|
|
@ -98,6 +115,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
|
|||
LL | fn ban<T>(x: T) where T: Trait<A = usize> {
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue