Auto merge of #54747 - levex:inline-asm-bad-operands, r=nagisa
codegen_llvm: verify that inline assembly operands are scalars Another set of inline assembly fixes. This time let's emit an error message when the operand value cannot be coerced into the operand constraint. Two questions: 1) Should I reuse `E0668` which was introduced in #54568 or just use `E0669` as it stands because they do mean different things, but maybe that's not too user-friendly. Just a thought. 2) The `try_fold` returns the operand which failed to be converted into a scalar value, any suggestions on how to use that in the error message? Thanks!
This commit is contained in:
commit
f42c510f2f
4 changed files with 122 additions and 7 deletions
57
src/test/ui/inline-asm-bad-operand.rs
Normal file
57
src/test/ui/inline-asm-bad-operand.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that the compiler will catch passing invalid values to inline assembly
|
||||
// operands.
|
||||
|
||||
#![feature(asm)]
|
||||
|
||||
#[repr(C)]
|
||||
struct MyPtr(usize);
|
||||
|
||||
fn main() {
|
||||
issue_37433();
|
||||
issue_37437();
|
||||
issue_40187();
|
||||
issue_54067();
|
||||
}
|
||||
|
||||
fn issue_37433() {
|
||||
unsafe {
|
||||
asm!("" :: "r"("")); //~ ERROR E0669
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let target = MyPtr(0);
|
||||
asm!("ret" : : "{rdi}"(target)); //~ ERROR E0669
|
||||
}
|
||||
}
|
||||
|
||||
fn issue_37437() {
|
||||
let hello: &str = "hello";
|
||||
// this should fail...
|
||||
unsafe { asm!("" :: "i"(hello)) }; //~ ERROR E0669
|
||||
// but this should succeed.
|
||||
unsafe { asm!("" :: "r"(hello.as_ptr())) };
|
||||
}
|
||||
|
||||
fn issue_40187() {
|
||||
let arr: [u8; 1] = [0; 1];
|
||||
unsafe {
|
||||
asm!("movups $1, %xmm0"::"m"(arr)); //~ ERROR E0669
|
||||
}
|
||||
}
|
||||
|
||||
fn issue_54067() {
|
||||
let addr: Option<u32> = Some(123);
|
||||
unsafe {
|
||||
asm!("mov sp, $0"::"r"(addr)); //~ ERROR E0669
|
||||
}
|
||||
}
|
||||
33
src/test/ui/inline-asm-bad-operand.stderr
Normal file
33
src/test/ui/inline-asm-bad-operand.stderr
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
error[E0669]: invalid value for constraint in inline assembly
|
||||
--> $DIR/inline-asm-bad-operand.rs:28:9
|
||||
|
|
||||
LL | asm!("" :: "r"("")); //~ ERROR E0669
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0669]: invalid value for constraint in inline assembly
|
||||
--> $DIR/inline-asm-bad-operand.rs:33:9
|
||||
|
|
||||
LL | asm!("ret" : : "{rdi}"(target)); //~ ERROR E0669
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0669]: invalid value for constraint in inline assembly
|
||||
--> $DIR/inline-asm-bad-operand.rs:40:14
|
||||
|
|
||||
LL | unsafe { asm!("" :: "i"(hello)) }; //~ ERROR E0669
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0669]: invalid value for constraint in inline assembly
|
||||
--> $DIR/inline-asm-bad-operand.rs:48:9
|
||||
|
|
||||
LL | asm!("movups $1, %xmm0"::"m"(arr)); //~ ERROR E0669
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0669]: invalid value for constraint in inline assembly
|
||||
--> $DIR/inline-asm-bad-operand.rs:55:9
|
||||
|
|
||||
LL | asm!("mov sp, $0"::"r"(addr)); //~ ERROR E0669
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0669`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue