Skip to content

Commit 3b7a14c

Browse files
committed
add const generic example
1 parent 95dc26c commit 3b7a14c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

compiler/rustc_passes/src/custom_abi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ impl<'tcx> Visitor<'tcx> for CheckCustomAbi<'tcx> {
8888
}
8989
}
9090

91+
// Because `extern "custom"` functions don't accept any parameter, and functions are
92+
// only methods when some variation on `self` is their first argument, methods cannot
93+
// use `extern "custom"` in a valid program.
9194
ExprKind::MethodCall(_, receiver, _, span) => {
9295
let opt_def_id = self
9396
.tcx
@@ -99,6 +102,7 @@ impl<'tcx> Visitor<'tcx> for CheckCustomAbi<'tcx> {
99102
(span, sig.abi)
100103
})
101104
}
105+
102106
_ => None,
103107
};
104108

tests/ui/abi/custom.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ trait BitwiseNot {
4747

4848
impl BitwiseNot for Thing {}
4949

50+
#[unsafe(naked)]
51+
unsafe extern "C" fn const_generic<const N: u64>() {
52+
naked_asm!(
53+
"mov rax, {}",
54+
"ret",
55+
const N,
56+
);
57+
}
58+
5059
pub fn main() {
5160
let mut x: u64 = 21;
5261
unsafe { asm!("call {}", sym double, inout("rax") x) };
@@ -71,4 +80,12 @@ pub fn main() {
7180
}
7281

7382
assert_eq!(caller(double, 2), 4);
83+
84+
let x: u64;
85+
unsafe { asm!("call {}", sym const_generic::<42>, out("rax") x) };
86+
assert_eq!(x, 42);
87+
88+
let x: u64;
89+
unsafe { asm!("call {}", sym const_generic::<84>, out("rax") x) };
90+
assert_eq!(x, 84);
7491
}

0 commit comments

Comments
 (0)