From 4d747128eb1e1c0da7432b071107d29721e19a04 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sun, 26 May 2024 00:11:32 +0100 Subject: [PATCH 1/9] Tweak type inference for `const` operands in inline asm Previously these would be treated like integer literals and default to `i32` if a type could not be determined. To allow for forward-compatibility with `str` constants in the future, this PR changes type inference to use an unbound type variable instead. The actual type checking is deferred until after typeck where we still ensure that the final type for the `const` operand is an integer type. --- .../src/check/intrinsicck.rs | 24 +++++++++++++++---- compiler/rustc_hir_typeck/src/lib.rs | 9 ++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs index 2e965c59ebb53..9286d3d83bffc 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs @@ -455,10 +455,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> { ); } } - // No special checking is needed for these: - // - Typeck has checked that Const operands are integers. - // - AST lowering guarantees that SymStatic points to a static. - hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {} + hir::InlineAsmOperand::Const { anon_const } => { + let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity(); + match ty.kind() { + ty::Error(_) => {} + ty::Int(_) | ty::Uint(_) => {} + _ => { + self.tcx + .dcx() + .struct_span_err(*op_sp, "invalid type for `const` operand") + .with_span_label( + self.tcx.def_span(anon_const.def_id), + format!("is {} `{}`", ty.kind().article(), ty), + ) + .with_help("`const` operands must be of an integer type") + .emit(); + } + }; + } + // AST lowering guarantees that SymStatic points to a static. + hir::InlineAsmOperand::SymStatic { .. } => {} // Check that sym actually points to a function. Later passes // depend on this. hir::InlineAsmOperand::SymFn { anon_const } => { diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 2c79366450909..ebb42e766701d 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -264,11 +264,10 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), span, .. }) | Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), span, .. }) => { asm.operands.iter().find_map(|(op, _op_sp)| match op { - hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => { - // Inline assembly constants must be integers. - Some(fcx.next_int_var()) - } - hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => { + hir::InlineAsmOperand::Const { anon_const } + | hir::InlineAsmOperand::SymFn { anon_const } + if anon_const.hir_id == id => + { Some(fcx.next_ty_var(span)) } _ => None, From 575fc72350d46030fd5e187b5d25fd455e43f406 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Tue, 11 Jun 2024 15:47:46 +0100 Subject: [PATCH 2/9] Work around #96304 by ignoring one test case --- tests/ui/asm/type-check-1.rs | 13 +++--- tests/ui/asm/type-check-1.stderr | 72 ++++++++++++++++---------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/tests/ui/asm/type-check-1.rs b/tests/ui/asm/type-check-1.rs index b0f1362f54334..09aa4a584ce12 100644 --- a/tests/ui/asm/type-check-1.rs +++ b/tests/ui/asm/type-check-1.rs @@ -55,11 +55,12 @@ fn main() { asm!("{}", const 0i32); asm!("{}", const 0i128); asm!("{}", const 0f32); - //~^ ERROR mismatched types + //~^ ERROR invalid type for `const` operand asm!("{}", const 0 as *mut u8); - //~^ ERROR mismatched types - asm!("{}", const &0); - //~^ ERROR mismatched types + //~^ ERROR invalid type for `const` operand + + // FIXME: Currently ICEs due to #96304 + //asm!("{}", const &0); } } @@ -73,6 +74,6 @@ global_asm!("{}", const 0); global_asm!("{}", const 0i32); global_asm!("{}", const 0i128); global_asm!("{}", const 0f32); -//~^ ERROR mismatched types +//~^ ERROR invalid type for `const` operand global_asm!("{}", const 0 as *mut u8); -//~^ ERROR mismatched types +//~^ ERROR invalid type for `const` operand diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index 1852623211813..30cd7fb42952e 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -39,6 +39,26 @@ LL | asm!("{}", sym x); | = help: `sym` operands must refer to either a function or a static +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:76:19 + | +LL | global_asm!("{}", const 0f32); + | ^^^^^^---- + | | + | is an `f32` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:78:19 + | +LL | global_asm!("{}", const 0 as *mut u8); + | ^^^^^^------------ + | | + | is a `*mut u8` + | + = help: `const` operands must be of an integer type + error: invalid asm output --> $DIR/type-check-1.rs:14:29 | @@ -102,49 +122,27 @@ LL | asm!("{}", inout(reg) v[..]); | = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly -error[E0308]: mismatched types - --> $DIR/type-check-1.rs:57:26 +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:57:20 | LL | asm!("{}", const 0f32); - | ^^^^ expected integer, found `f32` - -error[E0308]: mismatched types - --> $DIR/type-check-1.rs:59:26 - | -LL | asm!("{}", const 0 as *mut u8); - | ^^^^^^^^^^^^ expected integer, found `*mut u8` + | ^^^^^^---- + | | + | is an `f32` | - = note: expected type `{integer}` - found raw pointer `*mut u8` + = help: `const` operands must be of an integer type -error[E0308]: mismatched types - --> $DIR/type-check-1.rs:61:26 +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:59:20 | -LL | asm!("{}", const &0); - | ^^ expected integer, found `&{integer}` - | -help: consider removing the borrow - | -LL - asm!("{}", const &0); -LL + asm!("{}", const 0); - | - -error[E0308]: mismatched types - --> $DIR/type-check-1.rs:75:25 - | -LL | global_asm!("{}", const 0f32); - | ^^^^ expected integer, found `f32` - -error[E0308]: mismatched types - --> $DIR/type-check-1.rs:77:25 - | -LL | global_asm!("{}", const 0 as *mut u8); - | ^^^^^^^^^^^^ expected integer, found `*mut u8` +LL | asm!("{}", const 0 as *mut u8); + | ^^^^^^------------ + | | + | is a `*mut u8` | - = note: expected type `{integer}` - found raw pointer `*mut u8` + = help: `const` operands must be of an integer type -error: aborting due to 17 previous errors +error: aborting due to 16 previous errors -Some errors have detailed explanations: E0277, E0308, E0435. +Some errors have detailed explanations: E0277, E0435. For more information about an error, try `rustc --explain E0277`. From 97738e1b868567f0c1222179945ae3b387630c82 Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 21 Jul 2024 18:02:59 +0200 Subject: [PATCH 3/9] apply fix suggested by lcnr --- .../src/check/intrinsicck.rs | 52 ++++---------- .../rustc_hir_analysis/src/collect/type_of.rs | 72 +++++++++++++++++-- tests/ui/asm/type-check-1.rs | 4 +- tests/ui/asm/type-check-1.stderr | 52 ++++++++------ tests/ui/asm/x86_64/type-check-2.stderr | 32 ++++----- 5 files changed, 127 insertions(+), 85 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs index 9286d3d83bffc..6ea8e838d5cee 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs @@ -2,7 +2,7 @@ use rustc_ast::InlineAsmTemplatePiece; use rustc_data_structures::fx::FxIndexSet; use rustc_hir::{self as hir, LangItem}; use rustc_middle::bug; -use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy}; +use rustc_middle::ty::{self, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy}; use rustc_session::lint; use rustc_span::def_id::LocalDefId; use rustc_span::Symbol; @@ -455,48 +455,22 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> { ); } } + // Typeck has checked that Const operands are integers. hir::InlineAsmOperand::Const { anon_const } => { - let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity(); - match ty.kind() { - ty::Error(_) => {} - ty::Int(_) | ty::Uint(_) => {} - _ => { - self.tcx - .dcx() - .struct_span_err(*op_sp, "invalid type for `const` operand") - .with_span_label( - self.tcx.def_span(anon_const.def_id), - format!("is {} `{}`", ty.kind().article(), ty), - ) - .with_help("`const` operands must be of an integer type") - .emit(); - } - }; + debug_assert!(matches!( + self.tcx.type_of(anon_const.def_id).instantiate_identity().kind(), + ty::Error(_) | ty::Int(_) | ty::Uint(_) + )); } - // AST lowering guarantees that SymStatic points to a static. - hir::InlineAsmOperand::SymStatic { .. } => {} - // Check that sym actually points to a function. Later passes - // depend on this. + // Typeck has checked that SymFn refers to a function. hir::InlineAsmOperand::SymFn { anon_const } => { - let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity(); - match ty.kind() { - ty::Never | ty::Error(_) => {} - ty::FnDef(..) => {} - _ => { - self.tcx - .dcx() - .struct_span_err(*op_sp, "invalid `sym` operand") - .with_span_label( - self.tcx.def_span(anon_const.def_id), - format!("is {} `{}`", ty.kind().article(), ty), - ) - .with_help( - "`sym` operands must refer to either a function or a static", - ) - .emit(); - } - }; + debug_assert!(matches!( + self.tcx.type_of(anon_const.def_id).instantiate_identity().kind(), + ty::Error(_) | ty::Never | ty::FnDef(..) + )); } + // AST lowering guarantees that SymStatic points to a static. + hir::InlineAsmOperand::SymStatic { .. } => {} // No special checking is needed for labels. hir::InlineAsmOperand::Label { .. } => {} } diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 9affd654366f1..a0d438caf8f93 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -6,7 +6,7 @@ use rustc_hir::HirId; use rustc_middle::query::plumbing::CyclePlaceholder; use rustc_middle::ty::print::with_forced_trimmed_paths; use rustc_middle::ty::util::IntTypeExt; -use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; +use rustc_middle::ty::{self, Article, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::{bug, span_bug}; use rustc_span::symbol::Ident; use rustc_span::{Span, DUMMY_SP}; @@ -35,6 +35,20 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { let parent_node_id = tcx.parent_hir_id(hir_id); let parent_node = tcx.hir_node(parent_node_id); + let find_sym_fn = |&(op, op_sp)| match op { + hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == hir_id => { + Some((anon_const, op_sp)) + } + _ => None, + }; + + let find_const = |&(op, op_sp)| match op { + hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == hir_id => { + Some((anon_const, op_sp)) + } + _ => None, + }; + match parent_node { // Anon consts "inside" the type system. Node::ConstArg(&ConstArg { @@ -46,13 +60,57 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { // Anon consts outside the type system. Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. }) | Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. }) - if asm.operands.iter().any(|(op, _op_sp)| match op { - hir::InlineAsmOperand::Const { anon_const } - | hir::InlineAsmOperand::SymFn { anon_const } => anon_const.hir_id == hir_id, - _ => false, - }) => + if let Some((anon_const, op_sp)) = asm.operands.iter().find_map(find_sym_fn) => { - tcx.typeck(def_id).node_type(hir_id) + let ty = tcx.typeck(def_id).node_type(hir_id); + + match ty.kind() { + ty::Never | ty::Error(_) => ty, + ty::FnDef(..) => ty, + _ => { + tcx.dcx() + .struct_span_err(op_sp, "invalid `sym` operand") + .with_span_label( + tcx.def_span(anon_const.def_id), + format!("is {} `{}`", ty.kind().article(), ty), + ) + .with_help("`sym` operands must refer to either a function or a static") + .emit(); + + Ty::new_error_with_message( + tcx, + span, + format!("invalid type for `const` operand"), + ) + } + } + } + Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. }) + | Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. }) + if let Some((anon_const, op_sp)) = asm.operands.iter().find_map(find_const) => + { + let ty = tcx.typeck(def_id).node_type(hir_id); + + match ty.kind() { + ty::Error(_) => ty, + ty::Int(_) | ty::Uint(_) => ty, + _ => { + tcx.dcx() + .struct_span_err(op_sp, "invalid type for `const` operand") + .with_span_label( + tcx.def_span(anon_const.def_id), + format!("is {} `{}`", ty.kind().article(), ty), + ) + .with_help("`const` operands must be of an integer type") + .emit(); + + Ty::new_error_with_message( + tcx, + span, + format!("invalid type for `const` operand"), + ) + } + } } Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => { tcx.adt_def(tcx.hir().get_parent_item(hir_id)).repr().discr_type().to_ty(tcx) diff --git a/tests/ui/asm/type-check-1.rs b/tests/ui/asm/type-check-1.rs index 09aa4a584ce12..ad1a391539f1a 100644 --- a/tests/ui/asm/type-check-1.rs +++ b/tests/ui/asm/type-check-1.rs @@ -59,8 +59,8 @@ fn main() { asm!("{}", const 0 as *mut u8); //~^ ERROR invalid type for `const` operand - // FIXME: Currently ICEs due to #96304 - //asm!("{}", const &0); + asm!("{}", const &0); + //~^ ERROR invalid type for `const` operand } } diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index 30cd7fb42952e..bbed571284903 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -39,26 +39,6 @@ LL | asm!("{}", sym x); | = help: `sym` operands must refer to either a function or a static -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:76:19 - | -LL | global_asm!("{}", const 0f32); - | ^^^^^^---- - | | - | is an `f32` - | - = help: `const` operands must be of an integer type - -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:78:19 - | -LL | global_asm!("{}", const 0 as *mut u8); - | ^^^^^^------------ - | | - | is a `*mut u8` - | - = help: `const` operands must be of an integer type - error: invalid asm output --> $DIR/type-check-1.rs:14:29 | @@ -142,7 +122,37 @@ LL | asm!("{}", const 0 as *mut u8); | = help: `const` operands must be of an integer type -error: aborting due to 16 previous errors +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:62:20 + | +LL | asm!("{}", const &0); + | ^^^^^^-- + | | + | is a `&i32` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:76:19 + | +LL | global_asm!("{}", const 0f32); + | ^^^^^^---- + | | + | is an `f32` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/type-check-1.rs:78:19 + | +LL | global_asm!("{}", const 0 as *mut u8); + | ^^^^^^------------ + | | + | is a `*mut u8` + | + = help: `const` operands must be of an integer type + +error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0435. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/asm/x86_64/type-check-2.stderr b/tests/ui/asm/x86_64/type-check-2.stderr index 6ae118b16e766..e82a7c92664e5 100644 --- a/tests/ui/asm/x86_64/type-check-2.stderr +++ b/tests/ui/asm/x86_64/type-check-2.stderr @@ -6,22 +6,6 @@ LL | asm!("{}", sym x); | = help: `sym` operands must refer to either a function or a static -error: invalid `sym` operand - --> $DIR/type-check-2.rs:89:19 - | -LL | global_asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - -error: invalid `sym` operand - --> $DIR/type-check-2.rs:36:20 - | -LL | asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - error: arguments for inline assembly must be copyable --> $DIR/type-check-2.rs:43:32 | @@ -79,6 +63,14 @@ LL | asm!("{}", inout(reg) r); | = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly +error: invalid `sym` operand + --> $DIR/type-check-2.rs:36:20 + | +LL | asm!("{}", sym C); + | ^^^^^ is an `i32` + | + = help: `sym` operands must refer to either a function or a static + error[E0381]: used binding `x` isn't initialized --> $DIR/type-check-2.rs:15:28 | @@ -121,6 +113,14 @@ help: consider changing this to be mutable LL | let mut v: Vec = vec![0, 1, 2]; | +++ +error: invalid `sym` operand + --> $DIR/type-check-2.rs:89:19 + | +LL | global_asm!("{}", sym C); + | ^^^^^ is an `i32` + | + = help: `sym` operands must refer to either a function or a static + error: aborting due to 13 previous errors Some errors have detailed explanations: E0381, E0596. From c77b56901fc8098c36717a7620b1c89d3f01c14e Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 22 Jul 2024 20:44:32 +0200 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: Amanieu d'Antras --- compiler/rustc_hir_analysis/src/check/intrinsicck.rs | 2 +- compiler/rustc_hir_analysis/src/collect/type_of.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs index 6ea8e838d5cee..847a1e6470679 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs @@ -466,7 +466,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> { hir::InlineAsmOperand::SymFn { anon_const } => { debug_assert!(matches!( self.tcx.type_of(anon_const.def_id).instantiate_identity().kind(), - ty::Error(_) | ty::Never | ty::FnDef(..) + ty::Error(_) | ty::FnDef(..) )); } // AST lowering guarantees that SymStatic points to a static. diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index a0d438caf8f93..001b2190fc715 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -65,7 +65,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { let ty = tcx.typeck(def_id).node_type(hir_id); match ty.kind() { - ty::Never | ty::Error(_) => ty, + ty::Error(_) => ty, ty::FnDef(..) => ty, _ => { tcx.dcx() @@ -80,7 +80,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { Ty::new_error_with_message( tcx, span, - format!("invalid type for `const` operand"), + format!("invalid type for `sym` operand"), ) } } From d4ca1ac8b9d96ebb32b9bde8a7485b1ac857c10d Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Tue, 23 Jul 2024 23:31:34 +0100 Subject: [PATCH 5/9] rustfmt --- compiler/rustc_hir_analysis/src/collect/type_of.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 001b2190fc715..75826936c09fb 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -77,11 +77,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { .with_help("`sym` operands must refer to either a function or a static") .emit(); - Ty::new_error_with_message( - tcx, - span, - format!("invalid type for `sym` operand"), - ) + Ty::new_error_with_message(tcx, span, format!("invalid type for `sym` operand")) } } } From be66415e11234832b0ba10007d7445aef6213cb6 Mon Sep 17 00:00:00 2001 From: Folkert Date: Thu, 25 Jul 2024 10:29:59 +0200 Subject: [PATCH 6/9] use `ErrorGuaranteed` from emit --- compiler/rustc_hir_analysis/src/collect/type_of.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 75826936c09fb..8b5a8500f71e1 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -68,7 +68,8 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { ty::Error(_) => ty, ty::FnDef(..) => ty, _ => { - tcx.dcx() + let guar = tcx + .dcx() .struct_span_err(op_sp, "invalid `sym` operand") .with_span_label( tcx.def_span(anon_const.def_id), @@ -77,7 +78,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { .with_help("`sym` operands must refer to either a function or a static") .emit(); - Ty::new_error_with_message(tcx, span, format!("invalid type for `sym` operand")) + Ty::new_error(tcx, guar) } } } @@ -91,7 +92,8 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { ty::Error(_) => ty, ty::Int(_) | ty::Uint(_) => ty, _ => { - tcx.dcx() + let guar = tcx + .dcx() .struct_span_err(op_sp, "invalid type for `const` operand") .with_span_label( tcx.def_span(anon_const.def_id), @@ -100,11 +102,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { .with_help("`const` operands must be of an integer type") .emit(); - Ty::new_error_with_message( - tcx, - span, - format!("invalid type for `const` operand"), - ) + Ty::new_error(tcx, guar) } } } From b73077e3d8cc6f5174e0161ea02792c616189f59 Mon Sep 17 00:00:00 2001 From: Folkert Date: Thu, 1 Aug 2024 18:48:48 +0200 Subject: [PATCH 7/9] separate test file for invalid sym operand --- tests/ui/asm/aarch64/type-check-2.rs | 18 ---------- tests/ui/asm/aarch64/type-check-2.stderr | 34 +++++-------------- tests/ui/asm/invalid-sym-operand.rs | 30 +++++++++++++++++ tests/ui/asm/invalid-sym-operand.stderr | 26 +++++++++++++++ tests/ui/asm/type-check-1.rs | 6 ---- tests/ui/asm/type-check-1.stderr | 20 ++++------- tests/ui/asm/x86_64/type-check-2.rs | 20 ----------- tests/ui/asm/x86_64/type-check-2.stderr | 42 +++++------------------- 8 files changed, 80 insertions(+), 116 deletions(-) create mode 100644 tests/ui/asm/invalid-sym-operand.rs create mode 100644 tests/ui/asm/invalid-sym-operand.stderr diff --git a/tests/ui/asm/aarch64/type-check-2.rs b/tests/ui/asm/aarch64/type-check-2.rs index ba68cdd26d94c..46667ae3a6565 100644 --- a/tests/ui/asm/aarch64/type-check-2.rs +++ b/tests/ui/asm/aarch64/type-check-2.rs @@ -15,15 +15,6 @@ fn main() { unsafe { // Inputs must be initialized - // Sym operands must point to a function or static - - const C: i32 = 0; - static S: i32 = 0; - asm!("{}", sym S); - asm!("{}", sym main); - asm!("{}", sym C); - //~^ ERROR invalid `sym` operand - // Register operands must be Copy asm!("{:v}", in(vreg) SimdNonCopy(0.0, 0.0, 0.0, 0.0)); @@ -65,12 +56,3 @@ fn main() { asm!("{}", in(reg) u); } } - -// Sym operands must point to a function or static - -const C: i32 = 0; -static S: i32 = 0; -global_asm!("{}", sym S); -global_asm!("{}", sym main); -global_asm!("{}", sym C); -//~^ ERROR invalid `sym` operand diff --git a/tests/ui/asm/aarch64/type-check-2.stderr b/tests/ui/asm/aarch64/type-check-2.stderr index d647f6a9f0635..b7723fc74d4b6 100644 --- a/tests/ui/asm/aarch64/type-check-2.stderr +++ b/tests/ui/asm/aarch64/type-check-2.stderr @@ -1,29 +1,13 @@ -error: invalid `sym` operand - --> $DIR/type-check-2.rs:75:19 - | -LL | global_asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - -error: invalid `sym` operand - --> $DIR/type-check-2.rs:24:20 - | -LL | asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - error: arguments for inline assembly must be copyable - --> $DIR/type-check-2.rs:29:31 + --> $DIR/type-check-2.rs:20:31 | LL | asm!("{:v}", in(vreg) 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:41:28: 41:36}` for inline assembly - --> $DIR/type-check-2.rs:41:28 +error: cannot use value of type `{closure@$DIR/type-check-2.rs:32:28: 32:36}` for inline assembly + --> $DIR/type-check-2.rs:32:28 | LL | asm!("{}", in(reg) |x: i32| x); | ^^^^^^^^^^ @@ -31,7 +15,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` for inline assembly - --> $DIR/type-check-2.rs:43:28 + --> $DIR/type-check-2.rs:34:28 | LL | asm!("{}", in(reg) vec![0]); | ^^^^^^^ @@ -40,7 +24,7 @@ LL | asm!("{}", in(reg) vec![0]); = note: this error originates in the macro `vec` (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:45:28 + --> $DIR/type-check-2.rs:36:28 | LL | asm!("{}", in(reg) (1, 2, 3)); | ^^^^^^^^^ @@ -48,7 +32,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:47:28 + --> $DIR/type-check-2.rs:38:28 | LL | asm!("{}", in(reg) [1, 2, 3]); | ^^^^^^^^^ @@ -56,7 +40,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:55:31 + --> $DIR/type-check-2.rs:46:31 | LL | asm!("{}", inout(reg) f); | ^ @@ -64,12 +48,12 @@ 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:58:31 + --> $DIR/type-check-2.rs:49:31 | LL | asm!("{}", inout(reg) r); | ^ | = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly -error: aborting due to 9 previous errors +error: aborting due to 7 previous errors diff --git a/tests/ui/asm/invalid-sym-operand.rs b/tests/ui/asm/invalid-sym-operand.rs new file mode 100644 index 0000000000000..25e5fc6f9f6b7 --- /dev/null +++ b/tests/ui/asm/invalid-sym-operand.rs @@ -0,0 +1,30 @@ +use std::arch::{asm, global_asm}; + +fn main() { + unsafe { + // Sym operands must point to a function or static + + let x: u64 = 0; + const C: i32 = 0; + static S: i32 = 0; + asm!("{}", sym S); + asm!("{}", sym main); + asm!("{}", sym C); + //~^ ERROR invalid `sym` operand + asm!("{}", sym x); + //~^ ERROR invalid `sym` operand + } +} + +unsafe fn generic() { + asm!("{}", sym generic::); +} + +// Sym operands must point to a function or static + +const C: i32 = 0; +static S: i32 = 0; +global_asm!("{}", sym S); +global_asm!("{}", sym main); +global_asm!("{}", sym C); +//~^ ERROR invalid `sym` operand diff --git a/tests/ui/asm/invalid-sym-operand.stderr b/tests/ui/asm/invalid-sym-operand.stderr new file mode 100644 index 0000000000000..a0b97a96d8a51 --- /dev/null +++ b/tests/ui/asm/invalid-sym-operand.stderr @@ -0,0 +1,26 @@ +error: invalid `sym` operand + --> $DIR/invalid-sym-operand.rs:14:24 + | +LL | asm!("{}", sym x); + | ^ is a local variable + | + = help: `sym` operands must refer to either a function or a static + +error: invalid `sym` operand + --> $DIR/invalid-sym-operand.rs:12:20 + | +LL | asm!("{}", sym C); + | ^^^^^ is an `i32` + | + = help: `sym` operands must refer to either a function or a static + +error: invalid `sym` operand + --> $DIR/invalid-sym-operand.rs:29:19 + | +LL | global_asm!("{}", sym C); + | ^^^^^ is an `i32` + | + = help: `sym` operands must refer to either a function or a static + +error: aborting due to 3 previous errors + diff --git a/tests/ui/asm/type-check-1.rs b/tests/ui/asm/type-check-1.rs index ad1a391539f1a..2d25c6186e759 100644 --- a/tests/ui/asm/type-check-1.rs +++ b/tests/ui/asm/type-check-1.rs @@ -46,8 +46,6 @@ fn main() { asm!("{}", const const_bar(0)); asm!("{}", const const_bar(x)); //~^ ERROR attempt to use a non-constant value in a constant - asm!("{}", sym x); - //~^ ERROR invalid `sym` operand // Const operands must be integers and must be constants. @@ -64,10 +62,6 @@ fn main() { } } -unsafe fn generic() { - asm!("{}", sym generic::); -} - // Const operands must be integers and must be constants. global_asm!("{}", const 0); diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index bbed571284903..c2fc8d6690dff 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -31,14 +31,6 @@ help: consider using `const` instead of `let` LL | const x: /* Type */ = 0; | ~~~~~ ++++++++++++ -error: invalid `sym` operand - --> $DIR/type-check-1.rs:49:24 - | -LL | asm!("{}", sym x); - | ^ is a local variable - | - = help: `sym` operands must refer to either a function or a static - error: invalid asm output --> $DIR/type-check-1.rs:14:29 | @@ -103,7 +95,7 @@ LL | asm!("{}", inout(reg) v[..]); = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly error: invalid type for `const` operand - --> $DIR/type-check-1.rs:57:20 + --> $DIR/type-check-1.rs:55:20 | LL | asm!("{}", const 0f32); | ^^^^^^---- @@ -113,7 +105,7 @@ LL | asm!("{}", const 0f32); = help: `const` operands must be of an integer type error: invalid type for `const` operand - --> $DIR/type-check-1.rs:59:20 + --> $DIR/type-check-1.rs:57:20 | LL | asm!("{}", const 0 as *mut u8); | ^^^^^^------------ @@ -123,7 +115,7 @@ LL | asm!("{}", const 0 as *mut u8); = help: `const` operands must be of an integer type error: invalid type for `const` operand - --> $DIR/type-check-1.rs:62:20 + --> $DIR/type-check-1.rs:60:20 | LL | asm!("{}", const &0); | ^^^^^^-- @@ -133,7 +125,7 @@ LL | asm!("{}", const &0); = help: `const` operands must be of an integer type error: invalid type for `const` operand - --> $DIR/type-check-1.rs:76:19 + --> $DIR/type-check-1.rs:70:19 | LL | global_asm!("{}", const 0f32); | ^^^^^^---- @@ -143,7 +135,7 @@ LL | global_asm!("{}", const 0f32); = help: `const` operands must be of an integer type error: invalid type for `const` operand - --> $DIR/type-check-1.rs:78:19 + --> $DIR/type-check-1.rs:72:19 | LL | global_asm!("{}", const 0 as *mut u8); | ^^^^^^------------ @@ -152,7 +144,7 @@ LL | global_asm!("{}", const 0 as *mut u8); | = help: `const` operands must be of an integer type -error: aborting due to 17 previous errors +error: aborting due to 16 previous errors Some errors have detailed explanations: E0277, E0435. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/asm/x86_64/type-check-2.rs b/tests/ui/asm/x86_64/type-check-2.rs index 4b5d59fdbc785..ff811961462df 100644 --- a/tests/ui/asm/x86_64/type-check-2.rs +++ b/tests/ui/asm/x86_64/type-check-2.rs @@ -27,17 +27,6 @@ fn main() { asm!("{}", out(reg) v[0]); asm!("{}", inout(reg) v[0]); - // Sym operands must point to a function or static - - const C: i32 = 0; - static S: i32 = 0; - asm!("{}", sym S); - asm!("{}", sym main); - asm!("{}", sym C); - //~^ ERROR invalid `sym` operand - asm!("{}", sym x); - //~^ ERROR invalid `sym` operand - // Register operands must be Copy asm!("{}", in(xmm_reg) SimdNonCopy(0.0, 0.0, 0.0, 0.0)); @@ -79,12 +68,3 @@ fn main() { asm!("{}", in(reg) u); } } - -// Sym operands must point to a function or static - -const C: i32 = 0; -static S: i32 = 0; -global_asm!("{}", sym S); -global_asm!("{}", sym main); -global_asm!("{}", sym C); -//~^ ERROR invalid `sym` operand diff --git a/tests/ui/asm/x86_64/type-check-2.stderr b/tests/ui/asm/x86_64/type-check-2.stderr index e82a7c92664e5..c72e695aefb83 100644 --- a/tests/ui/asm/x86_64/type-check-2.stderr +++ b/tests/ui/asm/x86_64/type-check-2.stderr @@ -1,21 +1,13 @@ -error: invalid `sym` operand - --> $DIR/type-check-2.rs:38:24 - | -LL | asm!("{}", sym x); - | ^ is a local variable - | - = help: `sym` operands must refer to either a function or a static - error: arguments for inline assembly must be copyable - --> $DIR/type-check-2.rs:43:32 + --> $DIR/type-check-2.rs:32: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:55:28: 55:36}` for inline assembly - --> $DIR/type-check-2.rs:55:28 +error: cannot use value of type `{closure@$DIR/type-check-2.rs:44:28: 44:36}` for inline assembly + --> $DIR/type-check-2.rs:44:28 | LL | asm!("{}", in(reg) |x: i32| x); | ^^^^^^^^^^ @@ -23,7 +15,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` for inline assembly - --> $DIR/type-check-2.rs:57:28 + --> $DIR/type-check-2.rs:46:28 | LL | asm!("{}", in(reg) vec![0]); | ^^^^^^^ @@ -32,7 +24,7 @@ LL | asm!("{}", in(reg) vec![0]); = note: this error originates in the macro `vec` (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:59:28 + --> $DIR/type-check-2.rs:48:28 | LL | asm!("{}", in(reg) (1, 2, 3)); | ^^^^^^^^^ @@ -40,7 +32,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:61:28 + --> $DIR/type-check-2.rs:50:28 | LL | asm!("{}", in(reg) [1, 2, 3]); | ^^^^^^^^^ @@ -48,7 +40,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:69:31 + --> $DIR/type-check-2.rs:58:31 | LL | asm!("{}", inout(reg) f); | ^ @@ -56,21 +48,13 @@ 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:72:31 + --> $DIR/type-check-2.rs:61:31 | LL | asm!("{}", inout(reg) r); | ^ | = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly -error: invalid `sym` operand - --> $DIR/type-check-2.rs:36:20 - | -LL | asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - error[E0381]: used binding `x` isn't initialized --> $DIR/type-check-2.rs:15:28 | @@ -113,15 +97,7 @@ help: consider changing this to be mutable LL | let mut v: Vec = vec![0, 1, 2]; | +++ -error: invalid `sym` operand - --> $DIR/type-check-2.rs:89:19 - | -LL | global_asm!("{}", sym C); - | ^^^^^ is an `i32` - | - = help: `sym` operands must refer to either a function or a static - -error: aborting due to 13 previous errors +error: aborting due to 10 previous errors Some errors have detailed explanations: E0381, E0596. For more information about an error, try `rustc --explain E0381`. From 47e6db542e4a80b6c4b3aa4ab2eb2a991474f24f Mon Sep 17 00:00:00 2001 From: Folkert Date: Thu, 1 Aug 2024 18:57:59 +0200 Subject: [PATCH 8/9] separate test file for invalid const operand --- tests/ui/asm/invalid-const-operand.rs | 51 +++++++++++++ tests/ui/asm/invalid-const-operand.stderr | 86 ++++++++++++++++++++++ tests/ui/asm/invalid-sym-operand.rs | 18 ++--- tests/ui/asm/invalid-sym-operand.stderr | 14 ++-- tests/ui/asm/type-check-1.rs | 41 ----------- tests/ui/asm/type-check-1.stderr | 88 +---------------------- 6 files changed, 155 insertions(+), 143 deletions(-) create mode 100644 tests/ui/asm/invalid-const-operand.rs create mode 100644 tests/ui/asm/invalid-const-operand.stderr diff --git a/tests/ui/asm/invalid-const-operand.rs b/tests/ui/asm/invalid-const-operand.rs new file mode 100644 index 0000000000000..eff335ff6aaa3 --- /dev/null +++ b/tests/ui/asm/invalid-const-operand.rs @@ -0,0 +1,51 @@ +//@ needs-asm-support +//@ ignore-nvptx64 +//@ ignore-spirv + +#![feature(asm_const)] + +use std::arch::{asm, global_asm}; + +// Const operands must be integers and must be constants. + +global_asm!("{}", const 0); +global_asm!("{}", const 0i32); +global_asm!("{}", const 0i128); +global_asm!("{}", const 0f32); +//~^ ERROR invalid type for `const` operand +global_asm!("{}", const 0 as *mut u8); +//~^ ERROR invalid type for `const` operand + +fn main() { + unsafe { + // Const operands must be integers and must be constants. + + asm!("{}", const 0); + asm!("{}", const 0i32); + asm!("{}", const 0i128); + asm!("{}", const 0f32); + //~^ ERROR invalid type for `const` operand + asm!("{}", const 0 as *mut u8); + //~^ ERROR invalid type for `const` operand + asm!("{}", const &0); + //~^ ERROR invalid type for `const` operand + + // Constants must be... constant + + let x = 0; + const fn const_foo(x: i32) -> i32 { + x + } + const fn const_bar(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 + } +} diff --git a/tests/ui/asm/invalid-const-operand.stderr b/tests/ui/asm/invalid-const-operand.stderr new file mode 100644 index 0000000000000..a6d742b53c217 --- /dev/null +++ b/tests/ui/asm/invalid-const-operand.stderr @@ -0,0 +1,86 @@ +error[E0435]: attempt to use a non-constant value in a constant + --> $DIR/invalid-const-operand.rs:42:26 + | +LL | asm!("{}", const x); + | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ + +error[E0435]: attempt to use a non-constant value in a constant + --> $DIR/invalid-const-operand.rs:45:36 + | +LL | asm!("{}", const const_foo(x)); + | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ + +error[E0435]: attempt to use a non-constant value in a constant + --> $DIR/invalid-const-operand.rs:48:36 + | +LL | asm!("{}", const const_bar(x)); + | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ + +error: invalid type for `const` operand + --> $DIR/invalid-const-operand.rs:14:19 + | +LL | global_asm!("{}", const 0f32); + | ^^^^^^---- + | | + | is an `f32` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/invalid-const-operand.rs:16:19 + | +LL | global_asm!("{}", const 0 as *mut u8); + | ^^^^^^------------ + | | + | is a `*mut u8` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/invalid-const-operand.rs:26:20 + | +LL | asm!("{}", const 0f32); + | ^^^^^^---- + | | + | is an `f32` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/invalid-const-operand.rs:28:20 + | +LL | asm!("{}", const 0 as *mut u8); + | ^^^^^^------------ + | | + | is a `*mut u8` + | + = help: `const` operands must be of an integer type + +error: invalid type for `const` operand + --> $DIR/invalid-const-operand.rs:30:20 + | +LL | asm!("{}", const &0); + | ^^^^^^-- + | | + | is a `&i32` + | + = help: `const` operands must be of an integer type + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0435`. diff --git a/tests/ui/asm/invalid-sym-operand.rs b/tests/ui/asm/invalid-sym-operand.rs index 25e5fc6f9f6b7..69c4695125c43 100644 --- a/tests/ui/asm/invalid-sym-operand.rs +++ b/tests/ui/asm/invalid-sym-operand.rs @@ -1,5 +1,14 @@ use std::arch::{asm, global_asm}; +// Sym operands must point to a function or static + +const C: i32 = 0; +static S: i32 = 0; +global_asm!("{}", sym S); +global_asm!("{}", sym main); +global_asm!("{}", sym C); +//~^ ERROR invalid `sym` operand + fn main() { unsafe { // Sym operands must point to a function or static @@ -19,12 +28,3 @@ fn main() { unsafe fn generic() { asm!("{}", sym generic::); } - -// Sym operands must point to a function or static - -const C: i32 = 0; -static S: i32 = 0; -global_asm!("{}", sym S); -global_asm!("{}", sym main); -global_asm!("{}", sym C); -//~^ ERROR invalid `sym` operand diff --git a/tests/ui/asm/invalid-sym-operand.stderr b/tests/ui/asm/invalid-sym-operand.stderr index a0b97a96d8a51..8e0676200bb2e 100644 --- a/tests/ui/asm/invalid-sym-operand.stderr +++ b/tests/ui/asm/invalid-sym-operand.stderr @@ -1,5 +1,5 @@ error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:14:24 + --> $DIR/invalid-sym-operand.rs:23:24 | LL | asm!("{}", sym x); | ^ is a local variable @@ -7,18 +7,18 @@ LL | asm!("{}", sym x); = help: `sym` operands must refer to either a function or a static error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:12:20 + --> $DIR/invalid-sym-operand.rs:9:19 | -LL | asm!("{}", sym C); - | ^^^^^ is an `i32` +LL | global_asm!("{}", sym C); + | ^^^^^ is an `i32` | = help: `sym` operands must refer to either a function or a static error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:29:19 + --> $DIR/invalid-sym-operand.rs:21:20 | -LL | global_asm!("{}", sym C); - | ^^^^^ is an `i32` +LL | asm!("{}", sym C); + | ^^^^^ is an `i32` | = help: `sym` operands must refer to either a function or a static diff --git a/tests/ui/asm/type-check-1.rs b/tests/ui/asm/type-check-1.rs index 2d25c6186e759..22669dce280bb 100644 --- a/tests/ui/asm/type-check-1.rs +++ b/tests/ui/asm/type-check-1.rs @@ -28,46 +28,5 @@ fn main() { asm!("{}", inout(reg) v[..]); //~^ ERROR the size for values of type `[u64]` cannot be known at compilation time //~| ERROR cannot use value of type `[u64]` for inline assembly - - // Constants must be... constant - - let x = 0; - const fn const_foo(x: i32) -> i32 { - x - } - const fn const_bar(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 - - // Const operands must be integers and must be constants. - - asm!("{}", const 0); - asm!("{}", const 0i32); - asm!("{}", const 0i128); - asm!("{}", const 0f32); - //~^ ERROR invalid type for `const` operand - asm!("{}", const 0 as *mut u8); - //~^ ERROR invalid type for `const` operand - - asm!("{}", const &0); - //~^ ERROR invalid type for `const` operand } } - -// Const operands must be integers and must be constants. - -global_asm!("{}", const 0); -global_asm!("{}", const 0i32); -global_asm!("{}", const 0i128); -global_asm!("{}", const 0f32); -//~^ ERROR invalid type for `const` operand -global_asm!("{}", const 0 as *mut u8); -//~^ ERROR invalid type for `const` operand diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index c2fc8d6690dff..d47e6ae1d2a97 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -1,36 +1,3 @@ -error[E0435]: attempt to use a non-constant value in a constant - --> $DIR/type-check-1.rs:41:26 - | -LL | asm!("{}", const x); - | ^ non-constant value - | -help: consider using `const` instead of `let` - | -LL | const x: /* Type */ = 0; - | ~~~~~ ++++++++++++ - -error[E0435]: attempt to use a non-constant value in a constant - --> $DIR/type-check-1.rs:44:36 - | -LL | asm!("{}", const const_foo(x)); - | ^ non-constant value - | -help: consider using `const` instead of `let` - | -LL | const x: /* Type */ = 0; - | ~~~~~ ++++++++++++ - -error[E0435]: attempt to use a non-constant value in a constant - --> $DIR/type-check-1.rs:47:36 - | -LL | asm!("{}", const const_bar(x)); - | ^ non-constant value - | -help: consider using `const` instead of `let` - | -LL | const x: /* Type */ = 0; - | ~~~~~ ++++++++++++ - error: invalid asm output --> $DIR/type-check-1.rs:14:29 | @@ -94,57 +61,6 @@ LL | asm!("{}", inout(reg) v[..]); | = note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:55:20 - | -LL | asm!("{}", const 0f32); - | ^^^^^^---- - | | - | is an `f32` - | - = help: `const` operands must be of an integer type - -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:57:20 - | -LL | asm!("{}", const 0 as *mut u8); - | ^^^^^^------------ - | | - | is a `*mut u8` - | - = help: `const` operands must be of an integer type - -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:60:20 - | -LL | asm!("{}", const &0); - | ^^^^^^-- - | | - | is a `&i32` - | - = help: `const` operands must be of an integer type - -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:70:19 - | -LL | global_asm!("{}", const 0f32); - | ^^^^^^---- - | | - | is an `f32` - | - = help: `const` operands must be of an integer type - -error: invalid type for `const` operand - --> $DIR/type-check-1.rs:72:19 - | -LL | global_asm!("{}", const 0 as *mut u8); - | ^^^^^^------------ - | | - | is a `*mut u8` - | - = help: `const` operands must be of an integer type - -error: aborting due to 16 previous errors +error: aborting due to 8 previous errors -Some errors have detailed explanations: E0277, E0435. -For more information about an error, try `rustc --explain E0277`. +For more information about this error, try `rustc --explain E0277`. From eb726a50e65387c348ba56f2416fc3c0b649f95e Mon Sep 17 00:00:00 2001 From: Folkert Date: Fri, 2 Aug 2024 20:45:14 +0200 Subject: [PATCH 9/9] add `needs-asm-support` to invalid-sym-operand --- tests/ui/asm/invalid-sym-operand.rs | 4 ++++ tests/ui/asm/invalid-sym-operand.stderr | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/ui/asm/invalid-sym-operand.rs b/tests/ui/asm/invalid-sym-operand.rs index 69c4695125c43..2129c20b9681d 100644 --- a/tests/ui/asm/invalid-sym-operand.rs +++ b/tests/ui/asm/invalid-sym-operand.rs @@ -1,3 +1,7 @@ +//@ needs-asm-support +//@ ignore-nvptx64 +//@ ignore-spirv + use std::arch::{asm, global_asm}; // Sym operands must point to a function or static diff --git a/tests/ui/asm/invalid-sym-operand.stderr b/tests/ui/asm/invalid-sym-operand.stderr index 8e0676200bb2e..f0e6a17c25f6b 100644 --- a/tests/ui/asm/invalid-sym-operand.stderr +++ b/tests/ui/asm/invalid-sym-operand.stderr @@ -1,5 +1,5 @@ error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:23:24 + --> $DIR/invalid-sym-operand.rs:27:24 | LL | asm!("{}", sym x); | ^ is a local variable @@ -7,7 +7,7 @@ LL | asm!("{}", sym x); = help: `sym` operands must refer to either a function or a static error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:9:19 + --> $DIR/invalid-sym-operand.rs:13:19 | LL | global_asm!("{}", sym C); | ^^^^^ is an `i32` @@ -15,7 +15,7 @@ LL | global_asm!("{}", sym C); = help: `sym` operands must refer to either a function or a static error: invalid `sym` operand - --> $DIR/invalid-sym-operand.rs:21:20 + --> $DIR/invalid-sym-operand.rs:25:20 | LL | asm!("{}", sym C); | ^^^^^ is an `i32`