Skip to content

Commit b1c6c06

Browse files
committed
Permit asm_const and asm_sym to reference outer generic params
1 parent d93b037 commit b1c6c06

File tree

6 files changed

+60
-47
lines changed

6 files changed

+60
-47
lines changed

compiler/rustc_ast/src/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
326326
ItemKind::ForeignMod(ref foreign_module) => {
327327
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
328328
}
329-
ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
329+
ItemKind::GlobalAsm(ref asm) => visitor.visit_inline_asm(asm),
330330
ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
331331
visitor.visit_generics(generics);
332332
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
@@ -897,7 +897,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
897897
}
898898
ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
899899
ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
900-
ExprKind::InlineAsm(ref asm) => walk_inline_asm(visitor, asm),
900+
ExprKind::InlineAsm(ref asm) => visitor.visit_inline_asm(asm),
901901
ExprKind::Yield(ref optional_expression) => {
902902
walk_list!(visitor, visit_expr, optional_expression);
903903
}

compiler/rustc_resolve/src/ident.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ impl<'a> Resolver<'a> {
11711171
| AssocItemRibKind
11721172
| ModuleRibKind(..)
11731173
| MacroDefinition(..)
1174+
| InlineAsmSymRibKind
11741175
| ForwardGenericParamBanRibKind => {
11751176
// Nothing to do. Continue.
11761177
continue;
@@ -1216,22 +1217,6 @@ impl<'a> Resolver<'a> {
12161217
}
12171218
return Res::Err;
12181219
}
1219-
InlineAsmSymRibKind => {
1220-
let features = self.session.features_untracked();
1221-
if !features.generic_const_exprs {
1222-
if let Some(span) = finalize {
1223-
self.report_error(
1224-
span,
1225-
ResolutionError::ParamInNonTrivialAnonConst {
1226-
name: rib_ident.name,
1227-
is_type: true,
1228-
},
1229-
);
1230-
}
1231-
return Res::Err;
1232-
}
1233-
continue;
1234-
}
12351220
};
12361221

12371222
if let Some(span) = finalize {
@@ -1262,6 +1247,7 @@ impl<'a> Resolver<'a> {
12621247
| AssocItemRibKind
12631248
| ModuleRibKind(..)
12641249
| MacroDefinition(..)
1250+
| InlineAsmSymRibKind
12651251
| ForwardGenericParamBanRibKind => continue,
12661252

12671253
ConstantItemRibKind(trivial, _) => {
@@ -1296,22 +1282,6 @@ impl<'a> Resolver<'a> {
12961282
}
12971283
return Res::Err;
12981284
}
1299-
InlineAsmSymRibKind => {
1300-
let features = self.session.features_untracked();
1301-
if !features.generic_const_exprs {
1302-
if let Some(span) = finalize {
1303-
self.report_error(
1304-
span,
1305-
ResolutionError::ParamInNonTrivialAnonConst {
1306-
name: rib_ident.name,
1307-
is_type: false,
1308-
},
1309-
);
1310-
}
1311-
return Res::Err;
1312-
}
1313-
continue;
1314-
}
13151285
};
13161286

13171287
// This was an attempt to use a const parameter outside its scope.

compiler/rustc_resolve/src/late.rs

+23
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,29 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
918918
self.diagnostic_metadata.current_where_predicate = previous_value;
919919
}
920920

921+
fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
922+
for (op, _) in &asm.operands {
923+
match op {
924+
InlineAsmOperand::In { expr, .. }
925+
| InlineAsmOperand::Out { expr: Some(expr), .. }
926+
| InlineAsmOperand::InOut { expr, .. } => self.visit_expr(expr),
927+
InlineAsmOperand::Out { expr: None, .. } => {}
928+
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
929+
self.visit_expr(in_expr);
930+
if let Some(out_expr) = out_expr {
931+
self.visit_expr(out_expr);
932+
}
933+
}
934+
InlineAsmOperand::Const { anon_const, .. } => {
935+
// Although this is `DefKind::AnonConst`, it is allowed to reference outer
936+
// generic parameters like an inline const.
937+
self.resolve_inline_const(anon_const);
938+
}
939+
InlineAsmOperand::Sym { sym } => self.visit_inline_asm_sym(sym),
940+
}
941+
}
942+
}
943+
921944
fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
922945
// This is similar to the code for AnonConst.
923946
self.with_rib(ValueNS, InlineAsmSymRibKind, |this| {

src/test/ui/asm/generic-const.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// needs-asm-support
2+
// build-pass
3+
4+
#![feature(asm_const, asm_sym)]
5+
6+
use std::arch::asm;
7+
8+
fn foofoo<const N: usize>() {}
9+
10+
unsafe fn foo<const N: usize>() {
11+
asm!("/* {0} */", const N);
12+
asm!("/* {0} */", const N + 1);
13+
asm!("/* {0} */", sym foofoo::<N>);
14+
}
15+
16+
fn barbar<T>() {}
17+
18+
unsafe fn bar<T>() {
19+
asm!("/* {0} */", const std::mem::size_of::<T>());
20+
asm!("/* {0} */", const std::mem::size_of::<(T, T)>());
21+
asm!("/* {0} */", sym barbar::<T>);
22+
asm!("/* {0} */", sym barbar::<(T, T)>);
23+
}
24+
25+
fn main() {
26+
unsafe {
27+
foo::<0>();
28+
bar::<usize>();
29+
}
30+
}

src/test/ui/asm/type-check-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ fn main() {
6363

6464
unsafe fn generic<T>() {
6565
asm!("{}", sym generic::<T>);
66-
//~^ generic parameters may not be used in const operations
6766
}
6867

6968
// Const operands must be integers and must be constants.

src/test/ui/asm/type-check-1.stderr

+3-12
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ LL | asm!("{}", sym x);
3333
|
3434
= help: `sym` operands must refer to either a function or a static
3535

36-
error: generic parameters may not be used in const operations
37-
--> $DIR/type-check-1.rs:65:30
38-
|
39-
LL | asm!("{}", sym generic::<T>);
40-
| ^ cannot perform const operation using `T`
41-
|
42-
= note: type parameters may not be used in const expressions
43-
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
44-
4536
error[E0308]: mismatched types
4637
--> $DIR/type-check-1.rs:55:26
4738
|
@@ -109,21 +100,21 @@ LL | asm!("{}", inout(reg) v[..]);
109100
= note: all inline asm arguments must have a statically known size
110101

111102
error[E0308]: mismatched types
112-
--> $DIR/type-check-1.rs:74:25
103+
--> $DIR/type-check-1.rs:73:25
113104
|
114105
LL | global_asm!("{}", const 0f32);
115106
| ^^^^ expected integer, found `f32`
116107

117108
error[E0308]: mismatched types
118-
--> $DIR/type-check-1.rs:76:25
109+
--> $DIR/type-check-1.rs:75:25
119110
|
120111
LL | global_asm!("{}", const 0 as *mut u8);
121112
| ^^^^^^^^^^^^ expected integer, found *-ptr
122113
|
123114
= note: expected type `{integer}`
124115
found raw pointer `*mut u8`
125116

126-
error: aborting due to 15 previous errors
117+
error: aborting due to 14 previous errors
127118

128119
Some errors have detailed explanations: E0277, E0308, E0435.
129120
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)