Use AnonConst for asm! constants

This commit is contained in:
Amanieu d'Antras 2021-04-06 05:50:55 +01:00
parent cbd6ec7604
commit 32be124e30
37 changed files with 281 additions and 242 deletions

View File

@ -1998,7 +1998,7 @@ pub enum InlineAsmOperand {
out_expr: Option<P<Expr>>,
},
Const {
expr: P<Expr>,
anon_const: AnonConst,
},
Sym {
expr: P<Expr>,

View File

@ -1252,7 +1252,6 @@ pub fn noop_visit_expr<T: MutVisitor>(
match op {
InlineAsmOperand::In { expr, .. }
| InlineAsmOperand::InOut { expr, .. }
| InlineAsmOperand::Const { expr, .. }
| InlineAsmOperand::Sym { expr, .. } => vis.visit_expr(expr),
InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
@ -1265,6 +1264,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
vis.visit_expr(out_expr);
}
}
InlineAsmOperand::Const { anon_const, .. } => vis.visit_anon_const(anon_const),
}
}
}

View File

@ -835,7 +835,6 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
match op {
InlineAsmOperand::In { expr, .. }
| InlineAsmOperand::InOut { expr, .. }
| InlineAsmOperand::Const { expr, .. }
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
@ -848,6 +847,9 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
visitor.visit_expr(out_expr);
}
}
InlineAsmOperand::Const { anon_const, .. } => {
visitor.visit_anon_const(anon_const)
}
}
}
}

View File

@ -1411,9 +1411,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
}
}
InlineAsmOperand::Const { ref expr } => {
hir::InlineAsmOperand::Const { expr: self.lower_expr_mut(expr) }
}
InlineAsmOperand::Const { ref anon_const } => hir::InlineAsmOperand::Const {
anon_const: self.lower_anon_const(anon_const),
},
InlineAsmOperand::Sym { ref expr } => {
hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
}

View File

@ -2149,10 +2149,10 @@ impl<'a> State<'a> {
None => s.word("_"),
}
}
InlineAsmOperand::Const { expr } => {
InlineAsmOperand::Const { anon_const } => {
s.word("const");
s.space();
s.print_expr(expr);
s.print_expr(&anon_const.value);
}
InlineAsmOperand::Sym { expr } => {
s.word("sym");

View File

@ -136,8 +136,8 @@ fn parse_args<'a>(
ast::InlineAsmOperand::InOut { reg, expr, late: true }
}
} else if p.eat_keyword(kw::Const) {
let expr = p.parse_expr()?;
ast::InlineAsmOperand::Const { expr }
let anon_const = p.parse_anon_const_expr()?;
ast::InlineAsmOperand::Const { anon_const }
} else if p.eat_keyword(sym::sym) {
let expr = p.parse_expr()?;
match expr.kind {

View File

@ -822,11 +822,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
}
mir::InlineAsmOperand::Const { ref value } => {
if let mir::Operand::Constant(constant) = value {
let const_value = self
.eval_mir_constant(constant)
.eval_mir_constant(value)
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
let ty = constant.ty();
let ty = value.ty();
let size = bx.layout_of(ty).size;
let scalar = match const_value {
ConstValue::Scalar(s) => s,
@ -854,9 +853,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
_ => span_bug!(span, "asm const has bad type {}", ty),
};
InlineAsmOperandRef::Const { string }
} else {
span_bug!(span, "asm const is not a constant");
}
}
mir::InlineAsmOperand::SymFn { ref value } => {
let literal = self.monomorphize(value.literal);

View File

@ -2347,7 +2347,7 @@ pub enum InlineAsmOperand<'hir> {
out_expr: Option<Expr<'hir>>,
},
Const {
expr: Expr<'hir>,
anon_const: AnonConst,
},
Sym {
expr: Expr<'hir>,

View File

@ -1189,7 +1189,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
match op {
InlineAsmOperand::In { expr, .. }
| InlineAsmOperand::InOut { expr, .. }
| InlineAsmOperand::Const { expr, .. }
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
@ -1202,6 +1201,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
visitor.visit_expr(out_expr);
}
}
InlineAsmOperand::Const { anon_const, .. } => {
visitor.visit_anon_const(anon_const)
}
}
}
}

View File

@ -1570,10 +1570,10 @@ impl<'a> State<'a> {
None => s.word("_"),
}
}
hir::InlineAsmOperand::Const { expr } => {
hir::InlineAsmOperand::Const { anon_const } => {
s.word("const");
s.space();
s.print_expr(expr);
s.print_anon_const(anon_const);
}
hir::InlineAsmOperand::Sym { expr } => {
s.word("sym");

View File

@ -1213,7 +1213,7 @@ pub enum InlineAsmOperand<'tcx> {
out_place: Option<Place<'tcx>>,
},
Const {
value: Operand<'tcx>,
value: Box<Constant<'tcx>>,
},
SymFn {
value: Box<Constant<'tcx>>,

View File

@ -584,8 +584,7 @@ macro_rules! make_mir_visitor {
} => {
for op in operands {
match op {
InlineAsmOperand::In { value, .. }
| InlineAsmOperand::Const { value } => {
InlineAsmOperand::In { value, .. } => {
self.visit_operand(value, location);
}
InlineAsmOperand::Out { place, .. } => {
@ -607,7 +606,8 @@ macro_rules! make_mir_visitor {
);
}
}
InlineAsmOperand::SymFn { value } => {
InlineAsmOperand::Const { value }
| InlineAsmOperand::SymFn { value } => {
self.visit_constant(value, location);
}
InlineAsmOperand::SymStatic { def_id: _ } => {}

View File

@ -204,8 +204,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
} => {
for op in operands {
match *op {
InlineAsmOperand::In { reg: _, ref value }
| InlineAsmOperand::Const { ref value } => {
InlineAsmOperand::In { reg: _, ref value } => {
self.consume_operand(location, value);
}
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@ -219,7 +218,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
self.mutate_place(location, out_place, Shallow(None), JustWrite);
}
}
InlineAsmOperand::SymFn { value: _ }
InlineAsmOperand::Const { value: _ }
| InlineAsmOperand::SymFn { value: _ }
| InlineAsmOperand::SymStatic { def_id: _ } => {}
}
}

View File

@ -734,8 +734,7 @@ impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tc
} => {
for op in operands {
match *op {
InlineAsmOperand::In { reg: _, ref value }
| InlineAsmOperand::Const { ref value } => {
InlineAsmOperand::In { reg: _, ref value } => {
self.consume_operand(loc, (value, span), flow_state);
}
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@ -761,7 +760,8 @@ impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tc
);
}
}
InlineAsmOperand::SymFn { value: _ }
InlineAsmOperand::Const { value: _ }
| InlineAsmOperand::SymFn { value: _ }
| InlineAsmOperand::SymStatic { def_id: _ } => {}
}
}

View File

@ -425,7 +425,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
for op in operands {
match *op {
InlineAsmOperand::In { reg: _, ref value }
| InlineAsmOperand::Const { ref value } => {
=> {
self.gather_operand(value);
}
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@ -441,7 +441,8 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
self.gather_init(out_place.as_ref(), InitKind::Deep);
}
}
InlineAsmOperand::SymFn { value: _ }
InlineAsmOperand::Const { value: _ }
| InlineAsmOperand::SymFn { value: _ }
| InlineAsmOperand::SymStatic { def_id: _ } => {}
}
}

View File

@ -720,9 +720,6 @@ impl Conflicts<'a> {
}
}
}
InlineAsmOperand::Const { value } => {
assert!(value.place().is_none());
}
InlineAsmOperand::InOut {
reg: _,
late: _,
@ -731,6 +728,7 @@ impl Conflicts<'a> {
}
| InlineAsmOperand::In { reg: _, value: _ }
| InlineAsmOperand::Out { reg: _, late: _, place: None }
| InlineAsmOperand::Const { value: _ }
| InlineAsmOperand::SymFn { value: _ }
| InlineAsmOperand::SymStatic { def_id: _ } => {}
}

View File

@ -108,9 +108,6 @@ pub enum Candidate {
/// the attribute currently provides the semantic requirement that arguments
/// must be constant.
Argument { bb: BasicBlock, index: usize },
/// `const` operand in asm!.
InlineAsm { bb: BasicBlock, index: usize },
}
impl Candidate {
@ -118,16 +115,14 @@ impl Candidate {
fn forces_explicit_promotion(&self) -> bool {
match self {
Candidate::Ref(_) => false,
Candidate::Argument { .. } | Candidate::InlineAsm { .. } => true,
Candidate::Argument { .. } => true,
}
}
fn source_info(&self, body: &Body<'_>) -> SourceInfo {
match self {
Candidate::Ref(location) => *body.source_info(*location),
Candidate::Argument { bb, .. } | Candidate::InlineAsm { bb, .. } => {
*body.source_info(body.terminator_loc(*bb))
}
Candidate::Argument { bb, .. } => *body.source_info(body.terminator_loc(*bb)),
}
}
}
@ -217,16 +212,14 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
self.super_terminator(terminator, location);
match terminator.kind {
TerminatorKind::Call { ref func, .. } => {
if let TerminatorKind::Call { ref func, .. } = terminator.kind {
if let ty::FnDef(def_id, _) = *func.ty(self.ccx.body, self.ccx.tcx).kind() {
let fn_sig = self.ccx.tcx.fn_sig(def_id);
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = fn_sig.abi() {
let name = self.ccx.tcx.item_name(def_id);
// FIXME(eddyb) use `#[rustc_args_required_const(2)]` for shuffles.
if name.as_str().starts_with("simd_shuffle") {
self.candidates
.push(Candidate::Argument { bb: location.block, index: 2 });
self.candidates.push(Candidate::Argument { bb: location.block, index: 2 });
return; // Don't double count `simd_shuffle` candidates
}
@ -239,15 +232,6 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
}
}
}
TerminatorKind::InlineAsm { ref operands, .. } => {
for (index, op) in operands.iter().enumerate() {
if let InlineAsmOperand::Const { .. } = op {
self.candidates.push(Candidate::InlineAsm { bb: location.block, index })
}
}
}
_ => {}
}
}
}
@ -335,18 +319,6 @@ impl<'tcx> Validator<'_, 'tcx> {
_ => bug!(),
}
}
Candidate::InlineAsm { bb, index } => {
assert!(self.explicit);
let terminator = self.body[bb].terminator();
match &terminator.kind {
TerminatorKind::InlineAsm { operands, .. } => match &operands[index] {
InlineAsmOperand::Const { value } => self.validate_operand(value),
_ => bug!(),
},
_ => bug!(),
}
}
}
}
@ -818,9 +790,7 @@ pub fn validate_candidates(
}
match candidate {
Candidate::Argument { bb, index } | Candidate::InlineAsm { bb, index }
if !is_promotable =>
{
Candidate::Argument { bb, index } if !is_promotable => {
let span = ccx.body[bb].terminator().source_info.span;
let msg = format!("argument {} is required to be a constant", index + 1);
ccx.tcx.sess.span_err(span, &msg);
@ -1089,24 +1059,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
_ => bug!(),
}
}
Candidate::InlineAsm { bb, index } => {
let terminator = blocks[bb].terminator_mut();
match terminator.kind {
TerminatorKind::InlineAsm { ref mut operands, .. } => {
match &mut operands[index] {
InlineAsmOperand::Const { ref mut value } => {
let ty = value.ty(local_decls, self.tcx);
let span = terminator.source_info.span;
Rvalue::Use(mem::replace(value, promoted_operand(ty, span)))
}
_ => bug!(),
}
}
_ => bug!(),
}
}
}
};
@ -1161,7 +1113,7 @@ pub fn promote_candidates<'tcx>(
}
}
}
Candidate::Argument { .. } | Candidate::InlineAsm { .. } => {}
Candidate::Argument { .. } => {}
}
// Declare return place local so that `mir::Body::new` doesn't complain.

View File

@ -366,9 +366,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}),
}
}
thir::InlineAsmOperand::Const { expr } => mir::InlineAsmOperand::Const {
value: unpack!(block = this.as_local_operand(block, expr)),
},
thir::InlineAsmOperand::Const { value, span } => {
mir::InlineAsmOperand::Const {
value: box Constant { span, user_ty: None, literal: value.into() },
}
}
thir::InlineAsmOperand::SymFn { expr } => {
mir::InlineAsmOperand::SymFn { value: box this.as_constant(expr) }
}

View File

@ -503,8 +503,12 @@ impl<'thir, 'tcx> Cx<'thir, 'tcx> {
in_expr: self.mirror_expr(in_expr),
out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
},
hir::InlineAsmOperand::Const { ref expr } => {
InlineAsmOperand::Const { expr: self.mirror_expr(expr) }
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
let span = self.tcx.hir().span(anon_const.hir_id);
InlineAsmOperand::Const { value, span }
}
hir::InlineAsmOperand::Sym { ref expr } => {
let qpath = match expr.kind {

View File

@ -374,7 +374,8 @@ pub enum InlineAsmOperand<'thir, 'tcx> {
out_expr: Option<&'thir Expr<'thir, 'tcx>>,
},
Const {
expr: &'thir Expr<'thir, 'tcx>,
value: &'tcx Const<'tcx>,
span: Span,
},
SymFn {
expr: &'thir Expr<'thir, 'tcx>,

View File

@ -107,7 +107,7 @@ impl<'a> Parser<'a> {
}
}
pub(super) fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
pub fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
}

View File

@ -347,7 +347,7 @@ impl ExprVisitor<'tcx> {
}
fn check_asm(&self, asm: &hir::InlineAsm<'tcx>) {
for (idx, (op, _op_sp)) in asm.operands.iter().enumerate() {
for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
match *op {
hir::InlineAsmOperand::In { reg, ref expr } => {
self.check_asm_operand_type(idx, reg, expr, asm.template, None);
@ -372,14 +372,15 @@ impl ExprVisitor<'tcx> {
);
}
}
hir::InlineAsmOperand::Const { ref expr } => {
let ty = self.typeck_results.expr_ty_adjusted(expr);
match ty.kind() {
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
match value.ty.kind() {
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {}
_ => {
let msg =
"asm `const` arguments must be integer or floating-point values";
self.tcx.sess.span_err(expr.span, msg);
self.tcx.sess.span_err(*op_sp, msg);
}
}
}

View File

@ -1067,7 +1067,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
for (op, _op_sp) in asm.operands.iter().rev() {
match op {
hir::InlineAsmOperand::In { expr, .. }
| hir::InlineAsmOperand::Const { expr, .. }
| hir::InlineAsmOperand::Sym { expr, .. } => {
succ = self.propagate_through_expr(expr, succ)
}
@ -1085,6 +1084,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
succ = self.propagate_through_expr(in_expr, succ);
}
hir::InlineAsmOperand::Const { .. } => {}
}
}
succ

View File

@ -2087,7 +2087,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
for (op, _op_sp) in asm.operands {
match op {
hir::InlineAsmOperand::In { expr, .. } | hir::InlineAsmOperand::Const { expr } => {
hir::InlineAsmOperand::In { expr, .. } => {
self.check_expr_asm_operand(expr, true);
}
hir::InlineAsmOperand::Out { expr, .. } => {
@ -2104,6 +2104,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_expr_asm_operand(out_expr, false);
}
}
hir::InlineAsmOperand::Const { anon_const } => {
self.to_const(anon_const);
}
hir::InlineAsmOperand::Sym { expr } => {
self.check_expr(expr);
}

View File

@ -540,6 +540,19 @@ fn typeck_with_fallback<'tcx>(
kind: TypeVariableOriginKind::TypeInference,
span,
}),
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(ia), .. })
if ia.operands.iter().any(|(op, _op_sp)| match op {
hir::InlineAsmOperand::Const { anon_const } => {
anon_const.hir_id == id
}
_ => false,
}) =>
{
fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
})
}
_ => fallback(),
},
_ => fallback(),

View File

@ -430,6 +430,15 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
tcx.typeck(def_id).node_type(anon_const.hir_id)
}
Node::Expr(&Expr { kind: ExprKind::InlineAsm(ia), .. })
if ia.operands.iter().any(|(op, _op_sp)| match op {
hir::InlineAsmOperand::Const { anon_const } => anon_const.hir_id == hir_id,
_ => false,
}) =>
{
tcx.typeck(def_id).node_type(hir_id)
}
Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx
.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id())
.repr

View File

@ -313,7 +313,6 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
for (op, _op_sp) in asm.operands {
match op {
hir::InlineAsmOperand::In { expr, .. }
| hir::InlineAsmOperand::Const { expr, .. }
| hir::InlineAsmOperand::Sym { expr, .. } => self.consume_expr(expr),
hir::InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
@ -329,6 +328,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
self.mutate_expr(out_expr);
}
}
hir::InlineAsmOperand::Const { .. } => {}
}
}
}

View File

@ -4,38 +4,18 @@
#![feature(asm)]
use std::mem::size_of;
trait Proj {
const C: usize;
}
impl Proj for i8 {
const C: usize = 8;
}
impl Proj for i16 {
const C: usize = 16;
fn const_generic<const X: usize>() -> usize {
unsafe {
let a: usize;
asm!("mov {}, {}", out(reg) a, const X);
a
}
}
const fn constfn(x: usize) -> usize {
x
}
fn generic<T: Proj>() {
unsafe {
let a: usize;
asm!("mov {}, {}", out(reg) a, const size_of::<T>());
assert_eq!(a, size_of::<T>());
let b: usize;
asm!("mov {}, {}", out(reg) b, const size_of::<T>() + constfn(5));
assert_eq!(b, size_of::<T>() + 5);
let c: usize;
asm!("mov {}, {}", out(reg) c, const T::C);
assert_eq!(c, T::C);
}
}
fn main() {
unsafe {
let a: usize;
@ -51,6 +31,6 @@ fn main() {
assert_eq!(c, 10);
}
generic::<i8>();
generic::<i16>();
let d = const_generic::<5>();
assert_eq!(d, 5);
}

View File

@ -36,17 +36,23 @@ fn main() {
//~^ ERROR expected one of
asm!("{}", options(), const foo);
//~^ ERROR arguments are not allowed after options
//~^^ ERROR attempt to use a non-constant value in a constant
asm!("{a}", a = const foo, a = const bar);
//~^ ERROR duplicate argument named `a`
//~^^ ERROR argument never used
//~^^^ ERROR attempt to use a non-constant value in a constant
//~^^^^ ERROR attempt to use a non-constant value in a constant
asm!("", a = in("eax") foo);
//~^ ERROR explicit register arguments cannot have names
asm!("{a}", in("eax") foo, a = const bar);
//~^ ERROR named arguments cannot follow explicit register arguments
//~^^ ERROR attempt to use a non-constant value in a constant
asm!("{a}", in("eax") foo, a = const bar);
//~^ ERROR named arguments cannot follow explicit register arguments
//~^^ ERROR attempt to use a non-constant value in a constant
asm!("{1}", in("eax") foo, const bar);
//~^ ERROR positional arguments cannot follow named arguments or explicit register arguments
//~^^ ERROR attempt to use a non-constant value in a constant
asm!("", options(), "");
//~^ ERROR expected one of
asm!("{}", in(reg) foo, "{}", out(reg) foo);

View File

@ -91,7 +91,7 @@ LL | asm!("{}", options(), const foo);
| previous options
error: duplicate argument named `a`
--> $DIR/parse-error.rs:39:36
--> $DIR/parse-error.rs:40:36
|
LL | asm!("{a}", a = const foo, a = const bar);
| ------------- ^^^^^^^^^^^^^ duplicate argument
@ -99,7 +99,7 @@ LL | asm!("{a}", a = const foo, a = const bar);
| previously here
error: argument never used
--> $DIR/parse-error.rs:39:36
--> $DIR/parse-error.rs:40:36
|
LL | asm!("{a}", a = const foo, a = const bar);
| ^^^^^^^^^^^^^ argument never used
@ -107,13 +107,13 @@ LL | asm!("{a}", a = const foo, a = const bar);
= help: if this argument is intentionally unused, consider using it in an asm comment: `"/* {1} */"`
error: explicit register arguments cannot have names
--> $DIR/parse-error.rs:42:18
--> $DIR/parse-error.rs:45:18
|
LL | asm!("", a = in("eax") foo);
| ^^^^^^^^^^^^^^^^^
error: named arguments cannot follow explicit register arguments
--> $DIR/parse-error.rs:44:36
--> $DIR/parse-error.rs:47:36
|
LL | asm!("{a}", in("eax") foo, a = const bar);
| ------------- ^^^^^^^^^^^^^ named argument
@ -121,7 +121,7 @@ LL | asm!("{a}", in("eax") foo, a = const bar);
| explicit register argument
error: named arguments cannot follow explicit register arguments
--> $DIR/parse-error.rs:46:36
--> $DIR/parse-error.rs:50:36
|
LL | asm!("{a}", in("eax") foo, a = const bar);
| ------------- ^^^^^^^^^^^^^ named argument
@ -129,7 +129,7 @@ LL | asm!("{a}", in("eax") foo, a = const bar);
| explicit register argument
error: positional arguments cannot follow named arguments or explicit register arguments
--> $DIR/parse-error.rs:48:36
--> $DIR/parse-error.rs:53:36
|
LL | asm!("{1}", in("eax") foo, const bar);
| ------------- ^^^^^^^^^ positional argument
@ -137,19 +137,19 @@ LL | asm!("{1}", in("eax") foo, const bar);
| explicit register argument
error: expected one of `const`, `in`, `inlateout`, `inout`, `lateout`, `options`, `out`, or `sym`, found `""`
--> $DIR/parse-error.rs:50:29
--> $DIR/parse-error.rs:56:29
|
LL | asm!("", options(), "");
| ^^ expected one of 8 possible tokens
error: expected one of `const`, `in`, `inlateout`, `inout`, `lateout`, `options`, `out`, or `sym`, found `"{}"`
--> $DIR/parse-error.rs:52:33
--> $DIR/parse-error.rs:58:33
|
LL | asm!("{}", in(reg) foo, "{}", out(reg) foo);
| ^^^^ expected one of 8 possible tokens
error: asm template must be a string literal
--> $DIR/parse-error.rs:54:14
--> $DIR/parse-error.rs:60:14
|
LL | asm!(format!("{{{}}}", 0), in(reg) foo);
| ^^^^^^^^^^^^^^^^^^^^
@ -157,12 +157,67 @@ LL | asm!(format!("{{{}}}", 0), in(reg) foo);
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: asm template must be a string literal
--> $DIR/parse-error.rs:56:21
--> $DIR/parse-error.rs:62:21
|
LL | asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 25 previous errors
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:37:37
|
LL | let mut foo = 0;
| ---------- help: consider using `const` instead of `let`: `const foo`
...
LL | asm!("{}", options(), const foo);
| ^^^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:40:31
|
LL | let mut foo = 0;
| ---------- help: consider using `const` instead of `let`: `const foo`
...
LL | asm!("{a}", a = const foo, a = const bar);
| ^^^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:40:46
|
LL | let mut bar = 0;
| ---------- help: consider using `const` instead of `let`: `const bar`
...
LL | asm!("{a}", a = const foo, a = const bar);
| ^^^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:47:46
|
LL | let mut bar = 0;
| ---------- help: consider using `const` instead of `let`: `const bar`
...
LL | asm!("{a}", in("eax") foo, a = const bar);
| ^^^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:50:46
|
LL | let mut bar = 0;
| ---------- help: consider using `const` instead of `let`: `const bar`
...
LL | asm!("{a}", in("eax") foo, a = const bar);
| ^^^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/parse-error.rs:53:42
|
LL | let mut bar = 0;
| ---------- help: consider using `const` instead of `let`: `const bar`
...
LL | asm!("{1}", in("eax") foo, const bar);
| ^^^ non-constant value
error: aborting due to 31 previous errors
For more information about this error, try `rustc --explain E0435`.

View File

@ -21,5 +21,23 @@ fn main() {
//~^ ERROR the size for values of type `[u64]` cannot be known at compilation time
asm!("{}", inout(reg) v[..]);
//~^ ERROR the size for values of type `[u64]` cannot be known at compilation time
// Constants must be... constant
let x = 0;
const fn const_foo(x: i32) -> i32 {
x
}
const fn const_bar<T>(x: T) -> T {
x
}
asm!("{}", const x);
//~^ ERROR attempt to use a non-constant value in a constant
asm!("{}", const const_foo(0));
asm!("{}", const const_foo(x));
//~^ ERROR attempt to use a non-constant value in a constant
asm!("{}", const const_bar(0));
asm!("{}", const const_bar(x));
//~^ ERROR attempt to use a non-constant value in a constant
}
}

View File

@ -1,3 +1,30 @@
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/type-check-1.rs:34:26
|
LL | let x = 0;
| ----- help: consider using `const` instead of `let`: `const x`
...
LL | asm!("{}", const x);
| ^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/type-check-1.rs:37:36
|
LL | let x = 0;
| ----- help: consider using `const` instead of `let`: `const x`
...
LL | asm!("{}", const const_foo(x));
| ^ non-constant value
error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/type-check-1.rs:40:36
|
LL | let x = 0;
| ----- help: consider using `const` instead of `let`: `const x`
...
LL | asm!("{}", const const_bar(x));
| ^ non-constant value
error: invalid asm output
--> $DIR/type-check-1.rs:10:29
|
@ -37,6 +64,7 @@ LL | asm!("{}", inout(reg) v[..]);
= help: the trait `Sized` is not implemented for `[u64]`
= note: all inline asm arguments must have a statically known size
error: aborting due to 5 previous errors
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0277`.
Some errors have detailed explanations: E0277, E0435.
For more information about an error, try `rustc --explain E0277`.

View File

@ -28,31 +28,19 @@ fn main() {
// Const operands must be integer or floats, and must be constants.
let x = 0;
const C: i32 = 0;
const fn const_foo(x: i32) -> i32 {
x
}
const fn const_bar<T>(x: T) -> T {
x
}
asm!("{}", const 0);
asm!("{}", const 0i32);
asm!("{}", const 0f32);
asm!("{}", const 0 as *mut u8);
//~^ ERROR asm `const` arguments must be integer or floating-point values
asm!("{}", const &0);
//~^ ERROR asm `const` arguments must be integer or floating-point values
asm!("{}", const x);
//~^ ERROR argument 1 is required to be a constant
asm!("{}", const const_foo(0));
asm!("{}", const const_foo(x));
//~^ ERROR argument 1 is required to be a constant
asm!("{}", const const_bar(0));
asm!("{}", const const_bar(x));
//~^ ERROR argument 1 is required to be a constant
// This currently causes an ICE: https://github.com/rust-lang/rust/issues/81857
// asm!("{}", const &0);
// ERROR asm `const` arguments must be integer or floating-point values
// Sym operands must point to a function or static
const C: i32 = 0;
static S: i32 = 0;
asm!("{}", sym S);
asm!("{}", sym main);

View File

@ -1,25 +1,19 @@
error: asm `const` arguments must be integer or floating-point values
--> $DIR/type-check-2.rs:41:26
--> $DIR/type-check-2.rs:34:20
|
LL | asm!("{}", const 0 as *mut u8);
| ^^^^^^^^^^^^
error: asm `const` arguments must be integer or floating-point values
--> $DIR/type-check-2.rs:43:26
|
LL | asm!("{}", const &0);
| ^^
| ^^^^^^^^^^^^^^^^^^
error: arguments for inline assembly must be copyable
--> $DIR/type-check-2.rs:66:32
--> $DIR/type-check-2.rs:54:32
|
LL | asm!("{}", in(xmm_reg) SimdNonCopy(0.0, 0.0, 0.0, 0.0));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `SimdNonCopy` does not implement the Copy trait
error: cannot use value of type `[closure@$DIR/type-check-2.rs:78:28: 78:38]` for inline assembly
--> $DIR/type-check-2.rs:78:28
error: cannot use value of type `[closure@$DIR/type-check-2.rs:66:28: 66:38]` for inline assembly
--> $DIR/type-check-2.rs:66:28
|
LL | asm!("{}", in(reg) |x: i32| x);
| ^^^^^^^^^^
@ -27,7 +21,7 @@ LL | asm!("{}", in(reg) |x: i32| x);
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
error: cannot use value of type `Vec<i32>` for inline assembly
--> $DIR/type-check-2.rs:80:28
--> $DIR/type-check-2.rs:68:28
|
LL | asm!("{}", in(reg) vec![0]);
| ^^^^^^^
@ -36,7 +30,7 @@ LL | asm!("{}", in(reg) vec![0]);
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: cannot use value of type `(i32, i32, i32)` for inline assembly
--> $DIR/type-check-2.rs:82:28
--> $DIR/type-check-2.rs:70:28
|
LL | asm!("{}", in(reg) (1, 2, 3));
| ^^^^^^^^^
@ -44,7 +38,7 @@ LL | asm!("{}", in(reg) (1, 2, 3));
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
error: cannot use value of type `[i32; 3]` for inline assembly
--> $DIR/type-check-2.rs:84:28
--> $DIR/type-check-2.rs:72:28
|
LL | asm!("{}", in(reg) [1, 2, 3]);
| ^^^^^^^^^
@ -52,7 +46,7 @@ LL | asm!("{}", in(reg) [1, 2, 3]);
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
error: cannot use value of type `fn() {main}` for inline assembly
--> $DIR/type-check-2.rs:92:31
--> $DIR/type-check-2.rs:80:31
|
LL | asm!("{}", inout(reg) f);
| ^
@ -60,7 +54,7 @@ LL | asm!("{}", inout(reg) f);
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
error: cannot use value of type `&mut i32` for inline assembly
--> $DIR/type-check-2.rs:95:31
--> $DIR/type-check-2.rs:83:31
|
LL | asm!("{}", inout(reg) r);
| ^
@ -68,35 +62,17 @@ LL | asm!("{}", inout(reg) r);
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
error: asm `sym` operand must point to a fn or static
--> $DIR/type-check-2.rs:59:24
--> $DIR/type-check-2.rs:47:24
|
LL | asm!("{}", sym C);
| ^
error: asm `sym` operand must point to a fn or static
--> $DIR/type-check-2.rs:61:24
--> $DIR/type-check-2.rs:49:24
|
LL | asm!("{}", sym x);
| ^
error: argument 1 is required to be a constant
--> $DIR/type-check-2.rs:45:9
|
LL | asm!("{}", const x);
| ^^^^^^^^^^^^^^^^^^^^
error: argument 1 is required to be a constant
--> $DIR/type-check-2.rs:48:9
|
LL | asm!("{}", const const_foo(x));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: argument 1 is required to be a constant
--> $DIR/type-check-2.rs:51:9
|
LL | asm!("{}", const const_bar(x));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0381]: use of possibly-uninitialized variable: `x`
--> $DIR/type-check-2.rs:13:28
|
@ -127,7 +103,7 @@ LL | let v: Vec<u64> = vec![0, 1, 2];
LL | asm!("{}", inout(reg) v[0]);
| ^ cannot borrow as mutable
error: aborting due to 18 previous errors
error: aborting due to 14 previous errors
Some errors have detailed explanations: E0381, E0596.
For more information about an error, try `rustc --explain E0381`.

View File

@ -142,12 +142,12 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
.map(|(o, _)| match o {
InlineAsmOperand::In { expr, .. }
| InlineAsmOperand::InOut { expr, .. }
| InlineAsmOperand::Const { expr }
| InlineAsmOperand::Sym { expr } => never_loop_expr(expr, main_loop_id),
InlineAsmOperand::Out { expr, .. } => never_loop_expr_all(&mut expr.iter(), main_loop_id),
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
never_loop_expr_all(&mut once(in_expr).chain(out_expr.iter()), main_loop_id)
},
InlineAsmOperand::Const { .. } => NeverLoopResult::Otherwise,
})
.fold(NeverLoopResult::Otherwise, combine_both),
ExprKind::Struct(_, _, None)

View File

@ -306,7 +306,6 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
match op {
hir::InlineAsmOperand::In { expr, .. }
| hir::InlineAsmOperand::InOut { expr, .. }
| hir::InlineAsmOperand::Const { expr }
| hir::InlineAsmOperand::Sym { expr } => print_expr(cx, expr, indent + 1),
hir::InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
@ -319,6 +318,10 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
print_expr(cx, out_expr, indent + 1);
}
},
hir::InlineAsmOperand::Const { anon_const } => {
println!("{}anon_const:", ind);
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
}
}
}
},

View File

@ -663,7 +663,8 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
self.hash_expr(out_expr);
}
},
InlineAsmOperand::Const { expr } | InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
InlineAsmOperand::Const { anon_const } => self.hash_body(anon_const.body),
InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
}
}
},