Skip to content

Commit 95c04c4

Browse files
Make global_asm!() work
Because apparently, we were not accepting inline asm in item position, completely breaking it.
1 parent bd8087e commit 95c04c4

File tree

13 files changed

+58
-36
lines changed

13 files changed

+58
-36
lines changed

crates/hir-def/src/item_tree/lower.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ impl<'a> Ctx<'a> {
143143
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
144144
ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
145145
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
146+
// FIXME: Handle `global_asm!()`.
147+
ast::Item::AsmExpr(_) => return None,
146148
};
147149
let attrs = RawAttrs::new(self.db, item, self.span_map());
148150
self.add_attrs(mod_item.ast_id(), attrs);

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ macro_rules! global_asm {() => {}}
3333
#[rustc_builtin_macro]
3434
macro_rules! naked_asm {() => {}}
3535
36-
// FIXME: This creates an error
37-
// global_asm! {
38-
// ""
39-
// }
36+
global_asm! {
37+
""
38+
}
4039
4140
#[unsafe(naked)]
4241
extern "C" fn foo() {
@@ -64,10 +63,7 @@ macro_rules! global_asm {() => {}}
6463
#[rustc_builtin_macro]
6564
macro_rules! naked_asm {() => {}}
6665
67-
// FIXME: This creates an error
68-
// global_asm! {
69-
// ""
70-
// }
66+
builtin #global_asm ("")
7167
7268
#[unsafe(naked)]
7369
extern "C" fn foo() {

crates/hir-def/src/resolver.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,17 +1052,6 @@ impl<'db> Scope<'db> {
10521052
}
10531053
}
10541054

1055-
pub fn resolver_for_expr(
1056-
db: &dyn DefDatabase,
1057-
owner: DefWithBodyId,
1058-
expr_id: ExprId,
1059-
) -> Resolver<'_> {
1060-
let r = owner.resolver(db);
1061-
let scopes = db.expr_scopes(owner);
1062-
let scope_id = scopes.scope_for(expr_id);
1063-
resolver_for_scope_(db, scopes, scope_id, r, owner)
1064-
}
1065-
10661055
pub fn resolver_for_scope(
10671056
db: &dyn DefDatabase,
10681057
owner: DefWithBodyId,

crates/hir/src/source_analyzer.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,9 +1441,11 @@ fn scope_for(
14411441
) -> Option<ScopeId> {
14421442
node.ancestors_with_macros(db)
14431443
.take_while(|it| {
1444-
!ast::Item::can_cast(it.kind())
1445-
|| ast::MacroCall::can_cast(it.kind())
1446-
|| ast::Use::can_cast(it.kind())
1444+
let kind = it.kind();
1445+
!ast::Item::can_cast(kind)
1446+
|| ast::MacroCall::can_cast(kind)
1447+
|| ast::Use::can_cast(kind)
1448+
|| ast::AsmExpr::can_cast(kind)
14471449
})
14481450
.filter_map(|it| it.map(ast::Expr::cast).transpose())
14491451
.filter_map(|it| source_map.node_expr(it.as_ref())?.as_expr())

crates/parser/src/grammar/expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::grammar::attributes::ATTRIBUTE_FIRST;
44

55
use super::*;
66

7-
pub(super) use atom::{EXPR_RECOVERY_SET, LITERAL_FIRST, literal};
7+
pub(super) use atom::{EXPR_RECOVERY_SET, LITERAL_FIRST, literal, parse_asm_expr};
88
pub(crate) use atom::{block_expr, match_arm_list};
99

1010
#[derive(PartialEq, Eq)]

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn builtin_expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
328328
// tmp = out(reg) _,
329329
// );
330330
// }
331-
fn parse_asm_expr(p: &mut Parser<'_>, m: Marker) -> Option<CompletedMarker> {
331+
pub(crate) fn parse_asm_expr(p: &mut Parser<'_>, m: Marker) -> Option<CompletedMarker> {
332332
p.expect(T!['(']);
333333
if expr(p).is_none() {
334334
p.err_and_bump("expected asm template");

crates/parser/src/grammar/items.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ fn opt_item_without_modifiers(p: &mut Parser<'_>, m: Marker) -> Result<(), Marke
261261
T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
262262
T![static] if (la == IDENT || la == T![_] || la == T![mut]) => consts::static_(p, m),
263263

264+
IDENT
265+
if p.at_contextual_kw(T![builtin])
266+
&& p.nth_at(1, T![#])
267+
&& p.nth_at_contextual_kw(2, T![global_asm]) =>
268+
{
269+
p.bump_remap(T![builtin]);
270+
p.bump(T![#]);
271+
p.bump_remap(T![global_asm]);
272+
// test global_asm
273+
// builtin#global_asm("")
274+
expressions::parse_asm_expr(p, m);
275+
}
276+
264277
_ => return Err(m),
265278
};
266279
Ok(())

crates/parser/test_data/generated/runner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ mod ok {
300300
run_and_expect_no_errors("test_data/parser/inline/ok/generic_param_list.rs");
301301
}
302302
#[test]
303+
fn global_asm() { run_and_expect_no_errors("test_data/parser/inline/ok/global_asm.rs"); }
304+
#[test]
303305
fn half_open_range_pat() {
304306
run_and_expect_no_errors("test_data/parser/inline/ok/half_open_range_pat.rs");
305307
}

crates/parser/test_data/parser/inline/ok/asm_kinds.rast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ SOURCE_FILE
2323
R_PAREN ")"
2424
SEMICOLON ";"
2525
WHITESPACE "\n "
26-
EXPR_STMT
27-
ASM_EXPR
28-
BUILTIN_KW "builtin"
29-
POUND "#"
30-
GLOBAL_ASM_KW "global_asm"
31-
L_PAREN "("
32-
LITERAL
33-
STRING "\"\""
34-
R_PAREN ")"
35-
SEMICOLON ";"
26+
ASM_EXPR
27+
BUILTIN_KW "builtin"
28+
POUND "#"
29+
GLOBAL_ASM_KW "global_asm"
30+
L_PAREN "("
31+
LITERAL
32+
STRING "\"\""
33+
R_PAREN ")"
34+
SEMICOLON ";"
3635
WHITESPACE "\n "
3736
EXPR_STMT
3837
ASM_EXPR
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SOURCE_FILE
2+
ASM_EXPR
3+
BUILTIN_KW "builtin"
4+
POUND "#"
5+
GLOBAL_ASM_KW "global_asm"
6+
L_PAREN "("
7+
LITERAL
8+
STRING "\"\""
9+
R_PAREN ")"
10+
WHITESPACE "\n"

0 commit comments

Comments
 (0)