Skip to content

Commit

Permalink
review comments + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 16, 2023
1 parent a855178 commit a761f3d
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 20 deletions.
17 changes: 9 additions & 8 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Expand Up @@ -1340,7 +1340,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err,
path,
ty,
impl_ty,
Some(impl_ty),
item.kind,
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
sugg_span,
Expand Down Expand Up @@ -1377,7 +1377,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err,
path,
rcvr_ty,
rcvr_ty,
None,
item.kind,
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
sugg_span,
Expand Down Expand Up @@ -3148,7 +3148,7 @@ fn print_disambiguation_help<'tcx>(
err: &mut Diagnostic,
trait_name: String,
rcvr_ty: Ty<'_>,
self_ty: Ty<'_>,
impl_self_ty: Option<Ty<'_>>,
kind: ty::AssocKind,
def_kind_descr: &'static str,
span: Span,
Expand All @@ -3174,20 +3174,21 @@ fn print_disambiguation_help<'tcx>(
.collect::<Vec<_>>()
.join(", "),
);
let trait_name = if !fn_has_self_parameter {
format!("<{self_ty} as {trait_name}>")
let trait_name = if !fn_has_self_parameter && let Some(impl_self_ty) = impl_self_ty {
format!("<{impl_self_ty} as {trait_name}>")
} else {
trait_name
};
(span, format!("{trait_name}::{item_name}{args}"))
} else if let Some(impl_self_ty) = impl_self_ty {
(span.with_hi(item_name.span.lo()), format!("<{impl_self_ty} as {trait_name}>::"))
} else {
(span.with_hi(item_name.span.lo()), format!("<{self_ty} as {trait_name}>::"))
(span.with_hi(item_name.span.lo()), format!("{trait_name}::"))
};
err.span_suggestion_verbose(
span,
format!(
"disambiguate the {} for {}",
def_kind_descr,
"disambiguate the {def_kind_descr} for {}",
if let Some(candidate) = candidate {
format!("candidate #{candidate}")
} else {
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/methods/disambiguate-multiple-blanket-impl.rs
@@ -1,19 +1,27 @@
trait A {
type Type;
const CONST: usize;
fn foo(&self);
}

trait B {
type Type;
const CONST: usize;
fn foo(&self);
}

#[derive(Debug)]
struct S;

impl<T: std::fmt::Debug> A for T {
type Type = ();
const CONST: usize = 1; //~ NOTE candidate #1
fn foo(&self) {} //~ NOTE candidate #1
}

impl<T: std::fmt::Debug> B for T {
type Type = ();
const CONST: usize = 2; //~ NOTE candidate #2
fn foo(&self) {} //~ NOTE candidate #2
}

Expand All @@ -23,5 +31,10 @@ fn main() {
//~^ NOTE multiple `foo` found
//~| HELP disambiguate
//~| HELP disambiguate
S::CONST; //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `CONST` found
//~| HELP disambiguate
//~| HELP disambiguate
let _: S::Type; //~ ERROR ambiguous associated type
//~^ HELP use the fully-qualified path
}

49 changes: 44 additions & 5 deletions tests/ui/methods/disambiguate-multiple-blanket-impl.stderr
@@ -1,16 +1,29 @@
error[E0223]: ambiguous associated type
--> $DIR/disambiguate-multiple-blanket-impl.rs:38:12
|
LL | let _: S::Type;
| ^^^^^^^
|
help: use the fully-qualified path
|
LL | let _: <S as A>::Type;
| ~~~~~~~~~~~~~~
LL | let _: <S as B>::Type;
| ~~~~~~~~~~~~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-blanket-impl.rs:22:8
--> $DIR/disambiguate-multiple-blanket-impl.rs:30:8
|
LL | S::foo(&s);
| ^^^ multiple `foo` found
|
note: candidate #1 is defined in an impl of the trait `A` for the type `T`
--> $DIR/disambiguate-multiple-blanket-impl.rs:13:5
--> $DIR/disambiguate-multiple-blanket-impl.rs:19:5
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `T`
--> $DIR/disambiguate-multiple-blanket-impl.rs:17:5
--> $DIR/disambiguate-multiple-blanket-impl.rs:25:5
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
Expand All @@ -23,6 +36,32 @@ help: disambiguate the method for candidate #2
LL | <T as B>::foo(&s);
| ~~~~~~~~~~

error: aborting due to previous error
error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-blanket-impl.rs:34:8
|
LL | S::CONST;
| ^^^^^ multiple `CONST` found
|
note: candidate #1 is defined in an impl of the trait `A` for the type `T`
--> $DIR/disambiguate-multiple-blanket-impl.rs:18:5
|
LL | const CONST: usize = 1;
| ^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `T`
--> $DIR/disambiguate-multiple-blanket-impl.rs:24:5
|
LL | const CONST: usize = 2;
| ^^^^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #1
|
LL | <T as A>::CONST;
| ~~~~~~~~~~
help: disambiguate the associated constant for candidate #2
|
LL | <T as B>::CONST;
| ~~~~~~~~~~

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0034`.
Some errors have detailed explanations: E0034, E0223.
For more information about an error, try `rustc --explain E0034`.
15 changes: 14 additions & 1 deletion tests/ui/methods/disambiguate-multiple-impl.rs
@@ -1,18 +1,26 @@
trait A {
type Type;
const CONST: usize;
fn foo(&self);
}

trait B {
type Type;
const CONST: usize;
fn foo(&self);
}

struct S;

impl A for S {
type Type = ();
const CONST: usize = 1; //~ NOTE candidate #1
fn foo(&self) {} //~ NOTE candidate #1
}

impl B for S {
type Type = ();
const CONST: usize = 2; //~ NOTE candidate #2
fn foo(&self) {} //~ NOTE candidate #2
}

Expand All @@ -22,5 +30,10 @@ fn main() {
//~^ NOTE multiple `foo` found
//~| HELP disambiguate
//~| HELP disambiguate
let _: S::Type = (); //~ ERROR ambiguous associated type
//~| HELP use the fully-qualified path
let _ = S::CONST; //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `CONST` found
//~| HELP disambiguate
//~| HELP disambiguate
}

49 changes: 44 additions & 5 deletions tests/ui/methods/disambiguate-multiple-impl.stderr
@@ -1,16 +1,29 @@
error[E0223]: ambiguous associated type
--> $DIR/disambiguate-multiple-impl.rs:33:12
|
LL | let _: S::Type = ();
| ^^^^^^^
|
help: use the fully-qualified path
|
LL | let _: <S as A>::Type = ();
| ~~~~~~~~~~~~~~
LL | let _: <S as B>::Type = ();
| ~~~~~~~~~~~~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-impl.rs:21:8
--> $DIR/disambiguate-multiple-impl.rs:29:8
|
LL | S::foo(&s);
| ^^^ multiple `foo` found
|
note: candidate #1 is defined in an impl of the trait `A` for the type `S`
--> $DIR/disambiguate-multiple-impl.rs:12:5
--> $DIR/disambiguate-multiple-impl.rs:18:5
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `S`
--> $DIR/disambiguate-multiple-impl.rs:16:5
--> $DIR/disambiguate-multiple-impl.rs:24:5
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
Expand All @@ -23,6 +36,32 @@ help: disambiguate the method for candidate #2
LL | <S as B>::foo(&s);
| ~~~~~~~~~~

error: aborting due to previous error
error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-impl.rs:35:16
|
LL | let _ = S::CONST;
| ^^^^^ multiple `CONST` found
|
note: candidate #1 is defined in an impl of the trait `A` for the type `S`
--> $DIR/disambiguate-multiple-impl.rs:17:5
|
LL | const CONST: usize = 1;
| ^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `S`
--> $DIR/disambiguate-multiple-impl.rs:23:5
|
LL | const CONST: usize = 2;
| ^^^^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #1
|
LL | let _ = <S as A>::CONST;
| ~~~~~~~~~~
help: disambiguate the associated constant for candidate #2
|
LL | let _ = <S as B>::CONST;
| ~~~~~~~~~~

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0034`.
Some errors have detailed explanations: E0034, E0223.
For more information about an error, try `rustc --explain E0034`.
57 changes: 57 additions & 0 deletions tests/ui/methods/disambiguate-multiple-trait-2.rs
@@ -0,0 +1,57 @@
trait A {
type Type; //~ NOTE ambiguous `Type` from `A`
const CONST: usize = 1; //~ NOTE candidate #1
fn foo(&self); //~ NOTE candidate #1
}

trait B {
type Type; //~ NOTE ambiguous `Type` from `B`
const CONST: usize; //~ NOTE candidate #2
fn foo(&self); //~ NOTE candidate #2
}

trait C: A + B {}

fn a<T: C>(t: T) {
t.foo(); //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `foo` found
//~| HELP disambiguate the method
//~| HELP disambiguate the method
let _ = T::CONST; //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `CONST` found
//~| HELP disambiguate
//~| HELP disambiguate
let _: T::Type; //~ ERROR ambiguous associated type
//~^ NOTE ambiguous associated type `Type`
//~| HELP use fully qualified syntax
//~| HELP use fully qualified syntax
}

#[derive(Debug)]
struct S;

impl<T: std::fmt::Debug> A for T {
type Type = ();
const CONST: usize = 1; //~ NOTE candidate #1
fn foo(&self) {} //~ NOTE candidate #1
}

impl<T: std::fmt::Debug> B for T {
type Type = ();
const CONST: usize = 1; //~ NOTE candidate #2
fn foo(&self) {} //~ NOTE candidate #2
}

fn main() {
let s = S;
S::foo(&s); //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `foo` found
//~| HELP disambiguate
//~| HELP disambiguate
let _ = S::CONST; //~ ERROR multiple applicable items in scope
//~^ NOTE multiple `CONST` found
//~| HELP disambiguate
//~| HELP disambiguate
let _: S::Type; //~ ERROR ambiguous associated type
//~^ HELP use the fully-qualified path
}

0 comments on commit a761f3d

Please sign in to comment.