Skip to content

Commit

Permalink
Update exisgting instruction_set error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Aug 12, 2024
1 parent e9cae2b commit 92fbd2c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 52 deletions.
32 changes: 2 additions & 30 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 21 additions & 14 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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, _)),
Expand All @@ -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;
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/error-codes/E0778.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error[E0778]: `#[instruction_set]` requires an argument
error: `[instruction_set]` requires an argument
--> $DIR/E0778.rs:1:1
|
LL | #[instruction_set()]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0778`.
3 changes: 1 addition & 2 deletions tests/ui/error-codes/E0779.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error[E0779]: invalid instruction set specified
error: `[instruction_set]` attribute argument should be valid
--> $DIR/E0779.rs:1:1
|
LL | #[instruction_set(arm::magic)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0779`.

0 comments on commit 92fbd2c

Please sign in to comment.