Skip to content

Commit a23566a

Browse files
committed
Provide more context on resolve error caused from incorrect RTN
When encountering a resolve E0575 error for an associated method (when a type was expected), see if it could have been an intended return type notation bound. ``` error[E0575]: expected associated type, found associated function `Trait::method` --> $DIR/bad-inputs-and-output.rs:31:36 | LL | fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ not a associated type | help: you might have meant to use the return type notation syntax | LL - fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {} LL + fn foo_qualified<T: Trait>() where T::method(..): Send {} | ```
1 parent 81d8edc commit a23566a

File tree

7 files changed

+123
-17
lines changed

7 files changed

+123
-17
lines changed

compiler/rustc_ast_lowering/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
926926
if let Some(first_char) = constraint.ident.as_str().chars().next()
927927
&& first_char.is_ascii_lowercase()
928928
{
929-
tracing::info!(?data, ?data.inputs);
930929
let err = match (&data.inputs[..], &data.output) {
931930
([_, ..], FnRetTy::Default(_)) => {
932931
errors::BadReturnTypeNotation::Inputs { span: data.inputs_span }

compiler/rustc_ast_lowering/src/path.rs

-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
268268
}
269269
GenericArgs::Parenthesized(data) => match generic_args_mode {
270270
GenericArgsMode::ReturnTypeNotation => {
271-
tracing::info!(?data, ?data.inputs);
272271
let err = match (&data.inputs[..], &data.output) {
273272
([_, ..], FnRetTy::Default(_)) => {
274273
BadReturnTypeNotation::Inputs { span: data.inputs_span }

compiler/rustc_resolve/src/late.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ pub(crate) struct UnnecessaryQualification<'ra> {
623623
pub removal_span: Span,
624624
}
625625

626-
#[derive(Default)]
626+
#[derive(Default, Debug)]
627627
struct DiagMetadata<'ast> {
628628
/// The current trait's associated items' ident, used for diagnostic suggestions.
629629
current_trait_assoc_items: Option<&'ast [P<AssocItem>]>,
@@ -3146,6 +3146,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
31463146
PathSource::Trait(AliasPossibility::No),
31473147
Finalize::new(trait_ref.ref_id, trait_ref.path.span),
31483148
RecordPartialRes::Yes,
3149+
None,
31493150
);
31503151
self.diag_metadata.currently_processing_impl_trait = None;
31513152
if let Some(def_id) = res.expect_full_res().opt_def_id() {
@@ -4072,6 +4073,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
40724073
source,
40734074
Finalize::new(id, path.span),
40744075
RecordPartialRes::Yes,
4076+
None,
40754077
);
40764078
}
40774079

@@ -4083,14 +4085,21 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
40834085
source: PathSource<'ast>,
40844086
finalize: Finalize,
40854087
record_partial_res: RecordPartialRes,
4088+
parent_qself: Option<&QSelf>,
40864089
) -> PartialRes {
40874090
let ns = source.namespace();
40884091

40894092
let Finalize { node_id, path_span, .. } = finalize;
40904093
let report_errors = |this: &mut Self, res: Option<Res>| {
40914094
if this.should_report_errs() {
4092-
let (err, candidates) =
4093-
this.smart_resolve_report_errors(path, None, path_span, source, res);
4095+
let (err, candidates) = this.smart_resolve_report_errors(
4096+
path,
4097+
None,
4098+
path_span,
4099+
source,
4100+
res,
4101+
parent_qself,
4102+
);
40944103

40954104
let def_id = this.parent_scope.module.nearest_parent_mod();
40964105
let instead = res.is_some();
@@ -4159,6 +4168,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
41594168
path_span,
41604169
PathSource::Type,
41614170
None,
4171+
parent_qself,
41624172
);
41634173

41644174
// There are two different error messages user might receive at
@@ -4436,6 +4446,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44364446
PathSource::Trait(AliasPossibility::No),
44374447
Finalize::new(finalize.node_id, qself.path_span),
44384448
RecordPartialRes::No,
4449+
Some(&qself),
44394450
);
44404451

44414452
if trait_res.expect_full_res() == Res::Err {
@@ -4460,6 +4471,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44604471
PathSource::TraitItem(ns),
44614472
Finalize::with_root_span(finalize.node_id, finalize.path_span, qself.path_span),
44624473
RecordPartialRes::No,
4474+
Some(&qself),
44634475
);
44644476

44654477
// The remaining segments (the `C` in our example) will

compiler/rustc_resolve/src/late/diagnostics.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use super::NoConstantGenericsReason;
3535
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
3636
use crate::late::{
3737
AliasPossibility, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind,
38-
LifetimeUseSet, RibKind,
38+
LifetimeUseSet, QSelf, RibKind,
3939
};
4040
use crate::ty::fast_reject::SimplifiedType;
4141
use crate::{
@@ -421,6 +421,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
421421
span: Span,
422422
source: PathSource<'_>,
423423
res: Option<Res>,
424+
qself: Option<&QSelf>,
424425
) -> (Diag<'tcx>, Vec<ImportSuggestion>) {
425426
debug!(?res, ?source);
426427
let base_error = self.make_base_error(path, span, source, res);
@@ -453,6 +454,15 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
453454

454455
self.suggest_self_or_self_ref(&mut err, path, span);
455456
self.detect_assoc_type_constraint_meant_as_path(&mut err, &base_error);
457+
self.detect_rtn_with_fully_qualified_path(
458+
&mut err,
459+
path,
460+
following_seg,
461+
span,
462+
source,
463+
res,
464+
qself,
465+
);
456466
if self.suggest_self_ty(&mut err, source, path, span)
457467
|| self.suggest_self_value(&mut err, source, path, span)
458468
{
@@ -501,6 +511,33 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
501511
(err, candidates)
502512
}
503513

514+
fn detect_rtn_with_fully_qualified_path(
515+
&self,
516+
err: &mut Diag<'_>,
517+
path: &[Segment],
518+
following_seg: Option<&Segment>,
519+
span: Span,
520+
source: PathSource<'_>,
521+
res: Option<Res>,
522+
qself: Option<&QSelf>,
523+
) {
524+
if let Some(Res::Def(DefKind::AssocFn, _)) = res
525+
&& let PathSource::TraitItem(TypeNS) = source
526+
&& let None = following_seg
527+
&& let Some(qself) = qself
528+
&& let TyKind::Path(None, ty_path) = &qself.ty.kind
529+
&& ty_path.segments.len() == 1
530+
&& self.diag_metadata.current_where_predicate.is_some()
531+
{
532+
err.span_suggestion_verbose(
533+
span,
534+
"you might have meant to use the return type notation syntax",
535+
format!("{}::{}(..)", ty_path.segments[0].ident, path[path.len() - 1].ident),
536+
Applicability::MaybeIncorrect,
537+
);
538+
}
539+
}
540+
504541
fn detect_assoc_type_constraint_meant_as_path(
505542
&self,
506543
err: &mut Diag<'_>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//@ edition: 2021
2+
//@ run-rustfix
3+
#![feature(return_type_notation)]
4+
#![allow(dead_code)]
5+
6+
trait Trait {
7+
async fn method() {}
8+
}
9+
10+
fn foo<T: Trait<method(..): Send>>() {}
11+
//~^ ERROR argument types not allowed with return type notation
12+
13+
fn bar<T: Trait<method(..): Send>>() {}
14+
//~^ ERROR return type not allowed with return type notation
15+
16+
fn baz<T: Trait<method(..): Send>>() {}
17+
//~^ ERROR return type notation arguments must be elided with `..`
18+
19+
fn foo_path<T: Trait>() where T::method(..): Send {}
20+
//~^ ERROR argument types not allowed with return type notation
21+
22+
fn bar_path<T: Trait>() where T::method(..): Send {}
23+
//~^ ERROR return type not allowed with return type notation
24+
25+
fn bay_path<T: Trait>() where T::method(..): Send {}
26+
//~^ ERROR return type not allowed with return type notation
27+
28+
fn baz_path<T: Trait>() where T::method(..): Send {}
29+
//~^ ERROR return type notation arguments must be elided with `..`
30+
31+
fn foo_qualified<T: Trait>() where T::method(..): Send {}
32+
//~^ ERROR expected associated type
33+
34+
fn bar_qualified<T: Trait>() where T::method(..): Send {}
35+
//~^ ERROR expected associated type
36+
37+
fn baz_qualified<T: Trait>() where T::method(..): Send {}
38+
//~^ ERROR expected associated type
39+
40+
fn main() {}

tests/ui/associated-type-bounds/return-type-notation/bad-inputs-and-output.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ edition: 2021
2-
2+
//@ run-rustfix
33
#![feature(return_type_notation)]
4+
#![allow(dead_code)]
45

56
trait Trait {
67
async fn method() {}

tests/ui/associated-type-bounds/return-type-notation/bad-inputs-and-output.stderr

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: return type not allowed with return type notation
2-
--> $DIR/bad-inputs-and-output.rs:24:45
2+
--> $DIR/bad-inputs-and-output.rs:25:45
33
|
44
LL | fn bay_path<T: Trait>() where T::method(..) -> (): Send {}
55
| ^^^^^
@@ -11,25 +11,43 @@ LL + fn bay_path<T: Trait>() where T::method(..): Send {}
1111
|
1212

1313
error[E0575]: expected associated type, found associated function `Trait::method`
14-
--> $DIR/bad-inputs-and-output.rs:30:36
14+
--> $DIR/bad-inputs-and-output.rs:31:36
1515
|
1616
LL | fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a associated type
18+
|
19+
help: you might have meant to use the return type notation syntax
20+
|
21+
LL - fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {}
22+
LL + fn foo_qualified<T: Trait>() where T::method(..): Send {}
23+
|
1824

1925
error[E0575]: expected associated type, found associated function `Trait::method`
20-
--> $DIR/bad-inputs-and-output.rs:33:36
26+
--> $DIR/bad-inputs-and-output.rs:34:36
2127
|
2228
LL | fn bar_qualified<T: Trait>() where <T as Trait>::method() -> (): Send {}
2329
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a associated type
30+
|
31+
help: you might have meant to use the return type notation syntax
32+
|
33+
LL - fn bar_qualified<T: Trait>() where <T as Trait>::method() -> (): Send {}
34+
LL + fn bar_qualified<T: Trait>() where T::method(..): Send {}
35+
|
2436

2537
error[E0575]: expected associated type, found associated function `Trait::method`
26-
--> $DIR/bad-inputs-and-output.rs:36:36
38+
--> $DIR/bad-inputs-and-output.rs:37:36
2739
|
2840
LL | fn baz_qualified<T: Trait>() where <T as Trait>::method(): Send {}
2941
| ^^^^^^^^^^^^^^^^^^^^^^ not a associated type
42+
|
43+
help: you might have meant to use the return type notation syntax
44+
|
45+
LL - fn baz_qualified<T: Trait>() where <T as Trait>::method(): Send {}
46+
LL + fn baz_qualified<T: Trait>() where T::method(..): Send {}
47+
|
3048

3149
error: argument types not allowed with return type notation
32-
--> $DIR/bad-inputs-and-output.rs:9:23
50+
--> $DIR/bad-inputs-and-output.rs:10:23
3351
|
3452
LL | fn foo<T: Trait<method(i32): Send>>() {}
3553
| ^^^^^
@@ -41,7 +59,7 @@ LL + fn foo<T: Trait<method(..): Send>>() {}
4159
|
4260

4361
error: return type not allowed with return type notation
44-
--> $DIR/bad-inputs-and-output.rs:12:25
62+
--> $DIR/bad-inputs-and-output.rs:13:25
4563
|
4664
LL | fn bar<T: Trait<method() -> (): Send>>() {}
4765
| ^^^^^^
@@ -53,7 +71,7 @@ LL + fn bar<T: Trait<method(..): Send>>() {}
5371
|
5472

5573
error: return type notation arguments must be elided with `..`
56-
--> $DIR/bad-inputs-and-output.rs:15:23
74+
--> $DIR/bad-inputs-and-output.rs:16:23
5775
|
5876
LL | fn baz<T: Trait<method(): Send>>() {}
5977
| ^^
@@ -64,7 +82,7 @@ LL | fn baz<T: Trait<method(..): Send>>() {}
6482
| ++
6583

6684
error: argument types not allowed with return type notation
67-
--> $DIR/bad-inputs-and-output.rs:18:40
85+
--> $DIR/bad-inputs-and-output.rs:19:40
6886
|
6987
LL | fn foo_path<T: Trait>() where T::method(i32): Send {}
7088
| ^^^^^
@@ -76,7 +94,7 @@ LL + fn foo_path<T: Trait>() where T::method(..): Send {}
7694
|
7795

7896
error: return type not allowed with return type notation
79-
--> $DIR/bad-inputs-and-output.rs:21:42
97+
--> $DIR/bad-inputs-and-output.rs:22:42
8098
|
8199
LL | fn bar_path<T: Trait>() where T::method() -> (): Send {}
82100
| ^^^^^^
@@ -88,7 +106,7 @@ LL + fn bar_path<T: Trait>() where T::method(..): Send {}
88106
|
89107

90108
error: return type notation arguments must be elided with `..`
91-
--> $DIR/bad-inputs-and-output.rs:27:40
109+
--> $DIR/bad-inputs-and-output.rs:28:40
92110
|
93111
LL | fn baz_path<T: Trait>() where T::method(): Send {}
94112
| ^^

0 commit comments

Comments
 (0)