Skip to content

Commit 33dcc89

Browse files
Merge #9700
9700: fix: Remove the legacy macro scoping hack r=matklad a=jonas-schievink This stops prepending `self::` to single-ident macro paths, resolving even legacy-scoped macros using the fixed-point algorithm. This is not correct, but a lot easier than fixing this properly (which involves pushing a new scope for every macro definition and invocation). This allows resolution of macros from the prelude, fixing #9687. Co-authored-by: Jonas Schievink <[email protected]>
2 parents 068ede0 + 18b6327 commit 33dcc89

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

crates/hir_def/src/nameres/collector.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ impl ModCollector<'_, '_> {
18901890
}
18911891

18921892
fn collect_macro_call(&mut self, mac: &MacroCall) {
1893-
let mut ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, (*mac.path).clone());
1893+
let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, (*mac.path).clone());
18941894

18951895
// Case 1: try to resolve in legacy scope and expand macro_rules
18961896
let mut error = None;
@@ -1941,11 +1941,6 @@ impl ModCollector<'_, '_> {
19411941
}
19421942

19431943
// Case 2: resolve in module scope, expand during name resolution.
1944-
// We rewrite simple path `macro_name` to `self::macro_name` to force resolve in module scope only.
1945-
if ast_id.path.is_ident() {
1946-
ast_id.path.kind = PathKind::Super(0);
1947-
}
1948-
19491944
self.def_collector.unresolved_macros.push(MacroDirective {
19501945
module_id: self.module_id,
19511946
depth: self.macro_depth + 1,

crates/hir_def/src/nameres/tests/macros.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@ macro_rules! m {
349349
"#,
350350
expect![[r#"
351351
crate
352+
S: t v
352353
"#]],
353354
);
355+
// FIXME: should not expand. legacy macro scoping is not implemented.
354356
}
355357

356358
#[test]
@@ -427,6 +429,7 @@ macro_rules! baz {
427429
"#,
428430
expect![[r#"
429431
crate
432+
NotFoundBefore: t v
430433
Ok: t v
431434
OkAfter: t v
432435
OkShadowStop: t v
@@ -462,6 +465,7 @@ macro_rules! baz {
462465
crate::m3::m5
463466
"#]],
464467
);
468+
// FIXME: should not see `NotFoundBefore`
465469
}
466470

467471
#[test]
@@ -994,3 +998,26 @@ structs!(Foo);
994998
"#]],
995999
);
9961000
}
1001+
1002+
#[test]
1003+
fn macro_in_prelude() {
1004+
check(
1005+
r#"
1006+
//- /lib.rs crate:lib deps:std
1007+
global_asm!();
1008+
1009+
//- /std.rs crate:std
1010+
pub mod prelude {
1011+
pub mod rust_2018 {
1012+
pub macro global_asm() {
1013+
pub struct S;
1014+
}
1015+
}
1016+
}
1017+
"#,
1018+
expect![[r#"
1019+
crate
1020+
S: t v
1021+
"#]],
1022+
)
1023+
}

crates/ide_diagnostics/src/handlers/unresolved_macro_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ foo::bar!(92);
6363
macro_rules! m { () => {} }
6464
6565
m!(); m2!();
66-
//^^ error: unresolved macro `self::m2!`
66+
//^^ error: unresolved macro `m2!`
6767
"#,
6868
);
6969
}

0 commit comments

Comments
 (0)