Skip to content

Commit b34ce33

Browse files
Fix associated type errors too
1 parent c811f8f commit b34ce33

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ hir_analysis_variances_of = {$variances}
618618
hir_analysis_where_clause_on_main = `main` function is not allowed to have a `where` clause
619619
.label = `main` cannot have a `where` clause
620620
621+
hir_analysis_within_macro = within this macro
622+
621623
hir_analysis_wrong_number_of_generic_arguments_to_intrinsic =
622624
intrinsic has wrong number of {$descr} parameters: found {$found}, expected {$expected}
623625
.label = expected {$expected} {$descr} {$expected ->

compiler/rustc_hir_analysis/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ pub(crate) struct AssocItemNotFound<'a> {
8484
pub label: Option<AssocItemNotFoundLabel<'a>>,
8585
#[subdiagnostic]
8686
pub sugg: Option<AssocItemNotFoundSugg<'a>>,
87+
#[label(hir_analysis_within_macro)]
88+
pub within_macro_span: Option<Span>,
8789
}
8890

8991
#[derive(Subdiagnostic)]

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
151151
qself: &qself_str,
152152
label: None,
153153
sugg: None,
154+
// Try to get the span of the identifier within the path's syntax context
155+
// (if that's different).
156+
within_macro_span: assoc_name.span.within_macro(span),
154157
};
155158

156159
if is_dummy {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
trait Trait {}
2+
impl Trait for () {}
3+
4+
macro_rules! fully_qualified {
5+
($id:ident) => {
6+
<() as Trait>::$id
7+
}
8+
}
9+
10+
macro_rules! type_dependent {
11+
($t:ident, $id:ident) => {
12+
T::$id
13+
}
14+
}
15+
16+
fn t<T: Trait>() {
17+
let x: fully_qualified!(Assoc);
18+
//~^ ERROR cannot find associated type `Assoc` in trait `Trait`
19+
let x: type_dependent!(T, Assoc);
20+
//~^ ERROR associated type `Assoc` not found for `T`
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0576]: cannot find associated type `Assoc` in trait `Trait`
2+
--> $DIR/ident-from-macro-expansion.rs:17:29
3+
|
4+
LL | <() as Trait>::$id
5+
| --- within this macro
6+
...
7+
LL | let x: fully_qualified!(Assoc);
8+
| ^^^^^ not found in `Trait`
9+
10+
error[E0220]: associated type `Assoc` not found for `T`
11+
--> $DIR/ident-from-macro-expansion.rs:19:31
12+
|
13+
LL | T::$id
14+
| --- within this macro
15+
...
16+
LL | let x: type_dependent!(T, Assoc);
17+
| ^^^^^ associated type `Assoc` not found
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0220, E0576.
22+
For more information about an error, try `rustc --explain E0220`.

0 commit comments

Comments
 (0)