diff options
| author | He1pa <18012015693@163.com> | 2023-06-21 19:01:53 +0800 | 
|---|---|---|
| committer | He1pa <18012015693@163.com> | 2023-06-25 01:32:30 +0800 | 
| commit | 8af8a95a64cd765273aae3f4dc0aa50faab148c9 (patch) | |
| tree | 06252a6f90b3ff73ea2a1bb495c39f462f5863d9 /compiler/rustc_builtin_macros/src | |
| parent | ed1ce580ec27ffcfda1a95512e69185442f75ebe (diff) | |
| download | rust-8af8a95a64cd765273aae3f4dc0aa50faab148c9.tar.gz rust-8af8a95a64cd765273aae3f4dc0aa50faab148c9.zip | |
Migrate some rustc_builtin_macros to SessionDiagnostic
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/asm.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/cmdline_attrs.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/concat.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/concat_bytes.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/errors.rs | 88 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/global_allocator.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/proc_macro_harness.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/test.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/test_harness.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/trace_macros.rs | 3 | 
10 files changed, 109 insertions, 32 deletions
| diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 9734fc2b36d..5dcbb42ca84 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -371,11 +371,7 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a, p.expect(&token::OpenDelim(Delimiter::Parenthesis))?; if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) { - let err = p.sess.span_diagnostic.struct_span_err( - p.token.span, - "at least one abi must be provided as an argument to `clobber_abi`", - ); - return Err(err); + return Err(p.sess.span_diagnostic.create_err(errors::NonABI { span: p.token.span })); } let mut new_abis = Vec::new(); @@ -428,9 +424,9 @@ fn parse_reg<'a>( ast::InlineAsmRegOrRegClass::Reg(symbol) } _ => { - return Err( - p.struct_span_err(p.token.span, "expected register class or explicit register") - ); + return Err(p.sess.create_err(errors::ExpectedRegisterClassOrExplictRegister { + span: p.token.span, + })); } }; p.bump(); diff --git a/compiler/rustc_builtin_macros/src/cmdline_attrs.rs b/compiler/rustc_builtin_macros/src/cmdline_attrs.rs index 2b6fcc169be..7b75d7d84e4 100644 --- a/compiler/rustc_builtin_macros/src/cmdline_attrs.rs +++ b/compiler/rustc_builtin_macros/src/cmdline_attrs.rs @@ -1,5 +1,6 @@ //! Attributes injected into the crate root from command line using `-Z crate-attr`. +use crate::errors; use rustc_ast::attr::mk_attr; use rustc_ast::token; use rustc_ast::{self as ast, AttrItem, AttrStyle}; @@ -24,7 +25,9 @@ pub fn inject(krate: &mut ast::Crate, parse_sess: &ParseSess, attrs: &[String]) }; let end_span = parser.token.span; if parser.token != token::Eof { - parse_sess.span_diagnostic.span_err(start_span.to(end_span), "invalid crate attribute"); + parse_sess + .span_diagnostic + .emit_err(errors::InvalidCrateAttr { span: start_span.to(end_span) }); continue; } diff --git a/compiler/rustc_builtin_macros/src/concat.rs b/compiler/rustc_builtin_macros/src/concat.rs index 50e88ae2eee..9695fb4fee1 100644 --- a/compiler/rustc_builtin_macros/src/concat.rs +++ b/compiler/rustc_builtin_macros/src/concat.rs @@ -33,7 +33,7 @@ pub fn expand_concat( accumulator.push_str(&b.to_string()); } Ok(ast::LitKind::CStr(..)) => { - cx.span_err(e.span, "cannot concatenate a C string literal"); + cx.emit_err(errors::ConcatCStrLit{ span: e.span}); has_errors = true; } Ok(ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..)) => { diff --git a/compiler/rustc_builtin_macros/src/concat_bytes.rs b/compiler/rustc_builtin_macros/src/concat_bytes.rs index 5ef35af0a05..6a1586f071c 100644 --- a/compiler/rustc_builtin_macros/src/concat_bytes.rs +++ b/compiler/rustc_builtin_macros/src/concat_bytes.rs @@ -21,7 +21,7 @@ fn invalid_type_err( Ok(ast::LitKind::CStr(_, _)) => { // FIXME(c_str_literals): should concatenation of C string literals // include the null bytes in the end? - cx.span_err(span, "cannot concatenate C string literals"); + cx.emit_err(errors::ConcatCStrLit { span: span }); } Ok(ast::LitKind::Char(_)) => { let sugg = diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs index f1ab279daba..c3beb5da86e 100644 --- a/compiler/rustc_builtin_macros/src/errors.rs +++ b/compiler/rustc_builtin_macros/src/errors.rs @@ -88,6 +88,83 @@ pub(crate) struct ConcatBytestr { } #[derive(Diagnostic)] +#[diag(builtin_macros_concat_c_str_lit)] +pub(crate) struct ConcatCStrLit { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_export_macro_rules)] +pub(crate) struct ExportMacroRules { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_proc_macro)] +pub(crate) struct ProcMacro { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_invalid_crate_attribute)] +pub(crate) struct InvalidCrateAttr { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_non_abi)] +pub(crate) struct NonABI { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_trace_macros)] +pub(crate) struct TraceMacros { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_bench_sig)] +pub(crate) struct BenchSig { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_test_arg_non_lifetime)] +pub(crate) struct TestArgNonLifetime { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_should_panic)] +pub(crate) struct ShouldPanic { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_test_args)] +pub(crate) struct TestArgs { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] +#[diag(builtin_macros_alloc_must_statics)] +pub(crate) struct AllocMustStatics { + #[primary_span] + pub(crate) span: Span, +} + +#[derive(Diagnostic)] #[diag(builtin_macros_concat_bytes_invalid)] pub(crate) struct ConcatBytesInvalid { #[primary_span] @@ -202,6 +279,10 @@ pub(crate) struct BadDeriveTarget { } #[derive(Diagnostic)] +#[diag(builtin_macros_tests_not_support)] +pub(crate) struct TestsNotSupport {} + +#[derive(Diagnostic)] #[diag(builtin_macros_unexpected_lit, code = "E0777")] pub(crate) struct BadDeriveLit { #[primary_span] @@ -732,3 +813,10 @@ pub(crate) struct TestRunnerNargs { #[primary_span] pub(crate) span: Span, } + +#[derive(Diagnostic)] +#[diag(builtin_macros_expected_register_class_or_explict_register)] +pub(crate) struct ExpectedRegisterClassOrExplictRegister { + #[primary_span] + pub(crate) span: Span, +} diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index f0d378d12f7..5772471931f 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -1,5 +1,6 @@ use crate::util::check_builtin_macro_attribute; +use crate::errors; use rustc_ast::expand::allocator::{ global_fn_name, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS, }; @@ -34,7 +35,7 @@ pub fn expand( { (item, true, ecx.with_def_site_ctxt(ty.span)) } else { - ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics"); + ecx.sess.parse_sess.span_diagnostic.emit_err(errors::AllocMustStatics{span: item.span()}); return vec![orig_item]; }; diff --git a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs index 52b5601bb11..b35a2e2a292 100644 --- a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs +++ b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs @@ -1,3 +1,4 @@ +use crate::errors; use rustc_ast::ptr::P; use rustc_ast::visit::{self, Visitor}; use rustc_ast::{self as ast, attr, NodeId}; @@ -83,12 +84,7 @@ pub fn inject( impl<'a> CollectProcMacros<'a> { fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) { if self.is_proc_macro_crate && self.in_root && vis.kind.is_pub() { - self.handler.span_err( - sp, - "`proc-macro` crate types currently cannot export any items other \ - than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, \ - or `#[proc_macro_attribute]`", - ); + self.handler.emit_err(errors::ProcMacro { span: sp }); } } @@ -157,9 +153,9 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> { fn visit_item(&mut self, item: &'a ast::Item) { if let ast::ItemKind::MacroDef(..) = item.kind { if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) { - let msg = - "cannot export macro_rules! macros from a `proc-macro` crate type currently"; - self.handler.span_err(self.source_map.guess_head_span(item.span), msg); + self.handler.emit_err(errors::ExportMacroRules { + span: self.source_map.guess_head_span(item.span), + }); } } diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index d7a92dac50f..6bc4f6fc1fc 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -576,12 +576,7 @@ fn check_bench_signature( // N.B., inadequate check, but we're running // well before resolve, can't get too deep. if f.sig.decl.inputs.len() != 1 { - return Err(cx.sess.parse_sess.span_diagnostic.span_err( - i.span, - "functions used as benches must have \ - signature `fn(&mut Bencher) -> impl Termination`", - )); + return Err(cx.sess.parse_sess.span_diagnostic.emit_err(errors::BenchSig { span: i.span })); } - Ok(()) } diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index 9bc1e27b4ec..81b618548da 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -63,10 +63,7 @@ pub fn inject(krate: &mut ast::Crate, sess: &Session, resolver: &mut dyn Resolve // Silently allow compiling with panic=abort on these platforms, // but with old behavior (abort if a test fails). } else { - span_diagnostic.err( - "building tests with panic=abort is not supported \ - without `-Zpanic_abort_tests`", - ); + span_diagnostic.emit_err(errors::TestsNotSupport {}); } PanicStrategy::Unwind } diff --git a/compiler/rustc_builtin_macros/src/trace_macros.rs b/compiler/rustc_builtin_macros/src/trace_macros.rs index 9c98723e1f4..af1a392acc5 100644 --- a/compiler/rustc_builtin_macros/src/trace_macros.rs +++ b/compiler/rustc_builtin_macros/src/trace_macros.rs @@ -1,3 +1,4 @@ +use crate::errors; use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_expand::base::{self, ExtCtxt}; use rustc_span::symbol::kw; @@ -20,7 +21,7 @@ pub fn expand_trace_macros( }; err |= cursor.next().is_some(); if err { - cx.span_err(sp, "trace_macros! accepts only `true` or `false`") + cx.emit_err(errors::TraceMacros { span: sp }); } else { cx.set_trace_macros(value); } | 
