From 92fbd2cbd4c47973894db04fd0b6590366506498 Mon Sep 17 00:00:00 2001 From: chansuke Date: Mon, 12 Aug 2024 20:57:24 +0900 Subject: [PATCH] Update exisgting instruction_set error handling --- .../rustc_codegen_ssa/src/codegen_attrs.rs | 32 ++--------------- compiler/rustc_passes/messages.ftl | 12 ++++--- compiler/rustc_passes/src/check_attr.rs | 35 +++++++++++-------- compiler/rustc_passes/src/errors.rs | 7 ++++ tests/ui/error-codes/E0778.stderr | 3 +- tests/ui/error-codes/E0779.stderr | 3 +- 6 files changed, 40 insertions(+), 52 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs index 4ab20c154ccd0..80c012c83e756 100644 --- a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs +++ b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs @@ -397,38 +397,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs { unreachable!() } } - _ => { - struct_span_code_err!( - tcx.dcx(), - attr.span, - E0779, - "invalid instruction set specified", - ) - .emit(); - None - } + _ => None, } } - [] => { - struct_span_code_err!( - tcx.dcx(), - attr.span, - E0778, - "`#[instruction_set]` requires an argument" - ) - .emit(); - None - } - _ => { - struct_span_code_err!( - tcx.dcx(), - attr.span, - E0779, - "cannot specify more than one instruction set" - ) - .emit(); - None - } + _ => None, }) } sym::repr => { diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 5950a2c21d40a..296c7dfd68891 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -104,10 +104,6 @@ passes_coroutine_on_non_closure = attribute should be applied to closures .label = not a closure -passes_invalid_instruction_set = - `[instruction_set]` attribute argument should be valid - .label = `[instruction_set]` containes invalid argument - passes_coverage_not_fn_or_closure = attribute should be applied to a function definition or closure .label = not a function or closure @@ -288,6 +284,10 @@ passes_duplicate_lang_item_crate_depends = passes_empty_confusables = expected at least one confusable name +passes_empty_instruction_set = + `[instruction_set]` requires an argument + .label = `[instruction_set]` requires an argument + passes_export_name = attribute should be applied to a free function, impl method or static .label = not a free function, impl method or static @@ -377,6 +377,10 @@ passes_invalid_attr_at_crate_level = passes_invalid_attr_at_crate_level_item = the inner attribute doesn't annotate this {$kind} +passes_invalid_instruction_set = + `[instruction_set]` attribute argument should be valid + .label = `[instruction_set]` containes invalid argument + passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]` argument passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index b3e02614b7c2c..7771bbd25da28 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -4,9 +4,13 @@ //! conflicts between multiple such attributes attached to the same //! item. +use std::cell::Cell; +use std::collections::hash_map::Entry; + +use rustc_ast::token::TokenKind; +use rustc_ast::tokenstream::TokenTree; use rustc_ast::{ - ast, token::TokenKind, tokenstream::TokenTree, AttrKind, AttrStyle, Attribute, LitKind, - MetaItemKind, MetaItemLit, NestedMetaItem, + ast, AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem, }; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey}; @@ -36,8 +40,6 @@ use rustc_target::spec::abi::Abi; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs}; use rustc_trait_selection::traits::ObligationCtxt; -use std::cell::Cell; -use std::collections::hash_map::Entry; use tracing::debug; use crate::{errors, fluent_generated as fluent}; @@ -2360,7 +2362,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { // Valid item for `instruction_set()` is: // - arm::a32 // - arm::t32 - let valid_attribute = match (tokens.next(), tokens.next(), tokens.next()) { + match (tokens.next(), tokens.next(), tokens.next()) { ( Some(TokenTree::Token(first_token, _)), Some(TokenTree::Token(second_token, _)), @@ -2369,18 +2371,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> { (Some(first_ident), TokenKind::PathSep, Some(third_ident)) if first_ident.0.name == sym::arm => { - third_ident.0.name == sym::a32 || third_ident.0.name == sym::t32 + if third_ident.0.name == sym::a32 || third_ident.0.name == sym::t32 { + return; + } else { + self.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span }); + } + } + _ => { + self.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span }); } - _ => false, }, - _ => false, + (None, None, None) => { + self.dcx().emit_err(errors::EmptyInstructionSet { span: attr.span }); + } + _ => { + self.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span }); + } }; - - if !valid_attribute { - self.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span }); - } else { - return; - } } } } diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index b1b358097de23..47c1e86aed7b3 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -650,6 +650,13 @@ pub struct InvalidInstructionSet { pub span: Span, } +#[derive(Diagnostic)] +#[diag(passes_empty_instruction_set)] +pub struct EmptyInstructionSet { + #[primary_span] + pub span: Span, +} + #[derive(Diagnostic)] #[diag(passes_empty_confusables)] pub(crate) struct EmptyConfusables { diff --git a/tests/ui/error-codes/E0778.stderr b/tests/ui/error-codes/E0778.stderr index 7eb24c493bf54..3179e60980023 100644 --- a/tests/ui/error-codes/E0778.stderr +++ b/tests/ui/error-codes/E0778.stderr @@ -1,4 +1,4 @@ -error[E0778]: `#[instruction_set]` requires an argument +error: `[instruction_set]` requires an argument --> $DIR/E0778.rs:1:1 | LL | #[instruction_set()] @@ -6,4 +6,3 @@ LL | #[instruction_set()] error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0778`. diff --git a/tests/ui/error-codes/E0779.stderr b/tests/ui/error-codes/E0779.stderr index a01aa98b914c5..e7e74596eaabb 100644 --- a/tests/ui/error-codes/E0779.stderr +++ b/tests/ui/error-codes/E0779.stderr @@ -1,4 +1,4 @@ -error[E0779]: invalid instruction set specified +error: `[instruction_set]` attribute argument should be valid --> $DIR/E0779.rs:1:1 | LL | #[instruction_set(arm::magic)] @@ -6,4 +6,3 @@ LL | #[instruction_set(arm::magic)] error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0779`.