Skip to content

Commit f130244

Browse files
committed
Implement asm_const_ptr for inline asm
1 parent 6ce4c5d commit f130244

File tree

12 files changed

+99
-68
lines changed

12 files changed

+99
-68
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,14 +1231,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12311231
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
12321232
}
12331233
mir::InlineAsmOperand::Const { ref value } => {
1234-
let const_value = self.eval_mir_constant(value);
1235-
let string = common::asm_const_to_str(
1236-
bx.tcx(),
1237-
span,
1238-
const_value,
1239-
bx.layout_of(value.ty()),
1240-
);
1241-
InlineAsmOperandRef::Interpolate { string }
1234+
if value.ty().is_any_ptr() {
1235+
let value = self.eval_mir_constant_to_operand(bx, value);
1236+
InlineAsmOperandRef::Const { value }
1237+
} else {
1238+
let const_value = self.eval_mir_constant(value);
1239+
let string = common::asm_const_to_str(
1240+
bx.tcx(),
1241+
span,
1242+
const_value,
1243+
bx.layout_of(value.ty()),
1244+
);
1245+
InlineAsmOperandRef::Interpolate { string }
1246+
}
12421247
}
12431248
mir::InlineAsmOperand::SymFn { ref value } => {
12441249
let const_ = self.monomorphize(value.const_);

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ declare_features! (
368368
(unstable, arbitrary_self_types, "1.23.0", Some(44874)),
369369
/// Allows inherent and trait methods with arbitrary self types that are raw pointers.
370370
(unstable, arbitrary_self_types_pointers, "1.83.0", Some(44874)),
371+
/// Allows using `const` operands with pointer in inline assembly.
372+
(unstable, asm_const_ptr, "CURRENT_RUSTC_VERSION", Some(128464)),
371373
/// Enables experimental inline assembly support for additional architectures.
372374
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
373375
/// Enables experimental register support in inline assembly.

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ hir_analysis_ambiguous_assoc_item = ambiguous associated {$assoc_kind} `{$assoc_
44
hir_analysis_ambiguous_lifetime_bound =
55
ambiguous lifetime bound, explicit lifetime bound required
66
7+
hir_analysis_asm_const_ptr_unstable =
8+
using pointers in asm `const` operand is experimental
9+
710
hir_analysis_assoc_item_constraints_not_allowed_here =
811
associated item constraints are not allowed here
912
.label = associated item constraint not allowed here

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_target::asm::{
1515
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
1616
};
1717

18-
use crate::errors::RegisterTypeUnstable;
18+
use crate::errors::{AsmConstPtrUnstable, RegisterTypeUnstable};
1919

2020
pub struct InlineAsmCtxt<'a, 'tcx> {
2121
typing_env: ty::TypingEnv<'tcx>,
@@ -503,15 +503,46 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
503503
match ty.kind() {
504504
ty::Error(_) => {}
505505
_ if ty.is_integral() => {}
506+
ty::FnPtr(..) => {
507+
if !self.tcx().features().asm_const_ptr() {
508+
self.tcx()
509+
.sess
510+
.create_feature_err(
511+
AsmConstPtrUnstable { span: op_sp },
512+
sym::asm_const_ptr,
513+
)
514+
.emit();
515+
}
516+
}
517+
ty::RawPtr(pointee, _) | ty::Ref(_, pointee, _)
518+
if self.is_thin_ptr_ty(*pointee) =>
519+
{
520+
if !self.tcx().features().asm_const_ptr() {
521+
self.tcx()
522+
.sess
523+
.create_feature_err(
524+
AsmConstPtrUnstable { span: op_sp },
525+
sym::asm_const_ptr,
526+
)
527+
.emit();
528+
}
529+
}
506530
_ => {
531+
let const_possible_ty = if !self.tcx().features().asm_const_ptr() {
532+
"integer"
533+
} else {
534+
"integer or thin pointer"
535+
};
507536
self.infcx
508537
.dcx()
509538
.struct_span_err(op_sp, "invalid type for `const` operand")
510539
.with_span_label(
511540
self.tcx().def_span(anon_const.def_id),
512541
format!("is {} `{}`", ty.kind().article(), ty),
513542
)
514-
.with_help("`const` operands must be of an integer type")
543+
.with_help(format!(
544+
"`const` operands must be of an {const_possible_ty} type"
545+
))
515546
.emit();
516547
}
517548
}

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,13 @@ pub(crate) struct RegisterTypeUnstable<'a> {
16831683
pub ty: Ty<'a>,
16841684
}
16851685

1686+
#[derive(Diagnostic)]
1687+
#[diag(hir_analysis_asm_const_ptr_unstable)]
1688+
pub(crate) struct AsmConstPtrUnstable {
1689+
#[primary_span]
1690+
pub span: Span,
1691+
}
1692+
16861693
#[derive(LintDiagnostic)]
16871694
#[diag(hir_analysis_supertrait_item_shadowing)]
16881695
pub(crate) struct SupertraitItemShadowing {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ symbols! {
459459
as_str,
460460
asm,
461461
asm_const,
462+
asm_const_ptr,
462463
asm_experimental_arch,
463464
asm_experimental_reg,
464465
asm_goto,

tests/ui/asm/const-refs-to-static.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
//@ needs-asm-support
22
//@ ignore-nvptx64
33
//@ ignore-spirv
4+
//@ build-pass
5+
6+
#![feature(asm_const_ptr)]
47

58
use std::arch::{asm, global_asm};
69
use std::ptr::addr_of;
710

811
static FOO: u8 = 42;
912

10-
global_asm!("{}", const addr_of!(FOO));
11-
//~^ ERROR invalid type for `const` operand
13+
global_asm!("/* {} */", const addr_of!(FOO));
1214

1315
#[no_mangle]
1416
fn inline() {
15-
unsafe { asm!("{}", const addr_of!(FOO)) };
16-
//~^ ERROR invalid type for `const` operand
17+
unsafe { asm!("/* {} */", const addr_of!(FOO)) };
1718
}
1819

1920
fn main() {}

tests/ui/asm/const-refs-to-static.stderr

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/ui/asm/invalid-const-operand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//@ ignore-nvptx64
33
//@ ignore-spirv
44

5+
#![feature(asm_const_ptr)]
6+
57
use std::arch::{asm, global_asm};
68

79
// Const operands must be integers and must be constants.
@@ -12,7 +14,6 @@ global_asm!("{}", const 0i128);
1214
global_asm!("{}", const 0f32);
1315
//~^ ERROR invalid type for `const` operand
1416
global_asm!("{}", const 0 as *mut u8);
15-
//~^ ERROR invalid type for `const` operand
1617

1718
fn test1() {
1819
unsafe {
@@ -24,8 +25,7 @@ fn test1() {
2425
asm!("{}", const 0f32);
2526
//~^ ERROR invalid type for `const` operand
2627
asm!("{}", const 0 as *mut u8);
27-
//~^ ERROR invalid type for `const` operand
28-
asm!("{}", const &0);
28+
asm!("{}", const b"Foo".as_slice());
2929
//~^ ERROR invalid type for `const` operand
3030
}
3131
}

tests/ui/asm/invalid-const-operand.stderr

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,55 +35,35 @@ LL + const x: /* Type */ = 0;
3535
|
3636

3737
error: invalid type for `const` operand
38-
--> $DIR/invalid-const-operand.rs:12:19
38+
--> $DIR/invalid-const-operand.rs:14:19
3939
|
4040
LL | global_asm!("{}", const 0f32);
4141
| ^^^^^^----
4242
| |
4343
| is an `f32`
4444
|
45-
= help: `const` operands must be of an integer type
46-
47-
error: invalid type for `const` operand
48-
--> $DIR/invalid-const-operand.rs:14:19
49-
|
50-
LL | global_asm!("{}", const 0 as *mut u8);
51-
| ^^^^^^------------
52-
| |
53-
| is a `*mut u8`
54-
|
55-
= help: `const` operands must be of an integer type
45+
= help: `const` operands must be of an integer or thin pointer type
5646

5747
error: invalid type for `const` operand
58-
--> $DIR/invalid-const-operand.rs:24:20
48+
--> $DIR/invalid-const-operand.rs:25:20
5949
|
6050
LL | asm!("{}", const 0f32);
6151
| ^^^^^^----
6252
| |
6353
| is an `f32`
6454
|
65-
= help: `const` operands must be of an integer type
66-
67-
error: invalid type for `const` operand
68-
--> $DIR/invalid-const-operand.rs:26:20
69-
|
70-
LL | asm!("{}", const 0 as *mut u8);
71-
| ^^^^^^------------
72-
| |
73-
| is a `*mut u8`
74-
|
75-
= help: `const` operands must be of an integer type
55+
= help: `const` operands must be of an integer or thin pointer type
7656

7757
error: invalid type for `const` operand
7858
--> $DIR/invalid-const-operand.rs:28:20
7959
|
80-
LL | asm!("{}", const &0);
81-
| ^^^^^^--
60+
LL | asm!("{}", const b"Foo".as_slice());
61+
| ^^^^^^-----------------
8262
| |
83-
| is a `&i32`
63+
| is a `&[u8]`
8464
|
85-
= help: `const` operands must be of an integer type
65+
= help: `const` operands must be of an integer or thin pointer type
8666

87-
error: aborting due to 8 previous errors
67+
error: aborting due to 6 previous errors
8868

8969
For more information about this error, try `rustc --explain E0435`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ only-x86_64
2+
3+
use std::arch::asm;
4+
5+
fn main() {
6+
unsafe {
7+
asm!("/* {} */", const &0);
8+
//~^ ERROR using pointers in asm `const` operand is experimental
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: using pointers in asm `const` operand is experimental
2+
--> $DIR/feature-gate-asm_const_ptr.rs:7:26
3+
|
4+
LL | asm!("/* {} */", const &0);
5+
| ^^^^^^^^
6+
|
7+
= note: see issue #128464 <https://github.com/rust-lang/rust/issues/128464> for more information
8+
= help: add `#![feature(asm_const_ptr)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)