Skip to content

Commit f7f253e

Browse files
authored
Rollup merge of #102421 - lyming2007:issue-101866, r=lcnr
remove the unused :: between trait and type to give user correct diag… …nostic information modified: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs new file: src/test/ui/type/issue-101866.rs new file: src/test/ui/type/issue-101866.stderr
2 parents e2bc6b8 + 523a76a commit f7f253e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -2263,13 +2263,22 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
22632263
trait_impls.non_blanket_impls().len()
22642264
)
22652265
};
2266-
2266+
let mut suggestions = vec![(
2267+
trait_path_segment.ident.span.shrink_to_lo(),
2268+
format!("<{} as ", self.tcx.def_path(impl_def_id).to_string_no_crate_verbose())
2269+
)];
2270+
if let Some(generic_arg) = trait_path_segment.args {
2271+
let between_span = trait_path_segment.ident.span.between(generic_arg.span_ext);
2272+
// get rid of :: between Trait and <type>
2273+
// must be '::' between them, otherwise the parser won't accept the code
2274+
suggestions.push((between_span, "".to_string(),));
2275+
suggestions.push((generic_arg.span_ext.shrink_to_hi(), format!(">")));
2276+
} else {
2277+
suggestions.push((trait_path_segment.ident.span.shrink_to_hi(), format!(">")));
2278+
}
22672279
err.multipart_suggestion(
22682280
message,
2269-
vec![
2270-
(trait_path_segment.ident.span.shrink_to_lo(), format!("<{} as ", self.tcx.def_path(impl_def_id).to_string_no_crate_verbose())),
2271-
(trait_path_segment.ident.span.shrink_to_hi(), format!(">"))
2272-
],
2281+
suggestions,
22732282
Applicability::MaybeIncorrect
22742283
);
22752284
}

src/test/ui/type/issue-101866.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait TraitA<T> {
2+
fn func();
3+
}
4+
5+
struct StructA {}
6+
7+
impl TraitA<i32> for StructA {
8+
fn func() {}
9+
}
10+
11+
fn main() {
12+
TraitA::<i32>::func();
13+
//~^ ERROR: cannot call associated function on trait without specifying the corresponding `impl` type [E0790]
14+
//~| help: use the fully-qualified path to the only available implementation
15+
}

src/test/ui/type/issue-101866.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
2+
--> $DIR/issue-101866.rs:12:5
3+
|
4+
LL | fn func();
5+
| ---------- `TraitA::func` defined here
6+
...
7+
LL | TraitA::<i32>::func();
8+
| ^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
9+
|
10+
help: use the fully-qualified path to the only available implementation
11+
|
12+
LL - TraitA::<i32>::func();
13+
LL + <::StructA as TraitA<i32>>::func();
14+
|
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0790`.

0 commit comments

Comments
 (0)