Skip to content

Commit bd0a0ec

Browse files
authoredJun 23, 2022
Rollup merge of rust-lang#98353 - beetrees:builtin-macros-cfg-diag, r=davidtwco
Migrate two diagnostics from the `rustc_builtin_macros` crate Migrate two diagnostics to use the struct derive and be translatable. r? ``@davidtwco``
2 parents 1cead06 + be5337c commit bd0a0ec

File tree

8 files changed

+49
-7
lines changed

8 files changed

+49
-7
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3669,6 +3669,7 @@ dependencies = [
36693669
"rustc_feature",
36703670
"rustc_lexer",
36713671
"rustc_lint_defs",
3672+
"rustc_macros",
36723673
"rustc_parse",
36733674
"rustc_parse_format",
36743675
"rustc_session",

‎compiler/rustc_builtin_macros/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc_errors = { path = "../rustc_errors" }
1616
rustc_feature = { path = "../rustc_feature" }
1717
rustc_lexer = { path = "../rustc_lexer" }
1818
rustc_lint_defs = { path = "../rustc_lint_defs" }
19+
rustc_macros = { path = "../rustc_macros" }
1920
rustc_parse = { path = "../rustc_parse" }
2021
rustc_target = { path = "../rustc_target" }
2122
rustc_session = { path = "../rustc_session" }

‎compiler/rustc_builtin_macros/src/cfg.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::tokenstream::TokenStream;
88
use rustc_attr as attr;
99
use rustc_errors::PResult;
1010
use rustc_expand::base::{self, *};
11+
use rustc_macros::SessionDiagnostic;
1112
use rustc_span::Span;
1213

1314
pub fn expand_cfg(
@@ -34,21 +35,34 @@ pub fn expand_cfg(
3435
}
3536
}
3637

37-
fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
38+
#[derive(SessionDiagnostic)]
39+
#[error(slug = "builtin-macros-requires-cfg-pattern")]
40+
struct RequiresCfgPattern {
41+
#[primary_span]
42+
#[label]
43+
span: Span,
44+
}
45+
46+
#[derive(SessionDiagnostic)]
47+
#[error(slug = "builtin-macros-expected-one-cfg-pattern")]
48+
struct OneCfgPattern {
49+
#[primary_span]
50+
span: Span,
51+
}
52+
53+
fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
3854
let mut p = cx.new_parser_from_tts(tts);
3955

4056
if p.token == token::Eof {
41-
let mut err = cx.struct_span_err(sp, "macro requires a cfg-pattern as an argument");
42-
err.span_label(sp, "cfg-pattern required");
43-
return Err(err);
57+
return Err(cx.create_err(RequiresCfgPattern { span }));
4458
}
4559

4660
let cfg = p.parse_meta_item()?;
4761

4862
let _ = p.eat(&token::Comma);
4963

5064
if !p.eat(&token::Eof) {
51-
return Err(cx.struct_span_err(sp, "expected 1 cfg-pattern"));
65+
return Err(cx.create_err(OneCfgPattern { span }));
5266
}
5367

5468
Ok(cfg)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
builtin-macros-requires-cfg-pattern =
2+
macro requires a cfg-pattern as an argument
3+
.label = cfg-pattern required
4+
5+
builtin-macros-expected-one-cfg-pattern = expected 1 cfg-pattern

‎compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub use unic_langid::{langid, LanguageIdentifier};
3333
fluent_messages! {
3434
parser => "../locales/en-US/parser.ftl",
3535
typeck => "../locales/en-US/typeck.ftl",
36+
builtin_macros => "../locales/en-US/builtin_macros.ftl",
3637
}
3738

3839
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

‎compiler/rustc_expand/src/base.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
1414
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
1515
use rustc_lint_defs::BuiltinLintDiagnostics;
1616
use rustc_parse::{self, parser, MACRO_ARGUMENTS};
17-
use rustc_session::{parse::ParseSess, Limit, Session};
17+
use rustc_session::{parse::ParseSess, Limit, Session, SessionDiagnostic};
1818
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
1919
use rustc_span::edition::Edition;
2020
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
@@ -1085,6 +1085,17 @@ impl<'a> ExtCtxt<'a> {
10851085
self.sess.parse_sess.span_diagnostic.struct_span_err(sp, msg)
10861086
}
10871087

1088+
pub fn create_err(
1089+
&self,
1090+
err: impl SessionDiagnostic<'a>,
1091+
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
1092+
self.sess.create_err(err)
1093+
}
1094+
1095+
pub fn emit_err(&self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
1096+
self.sess.emit_err(err)
1097+
}
1098+
10881099
/// Emit `msg` attached to `sp`, without immediately stopping
10891100
/// compilation.
10901101
///

‎src/test/ui/macros/cfg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ fn main() {
22
cfg!(); //~ ERROR macro requires a cfg-pattern
33
cfg!(123); //~ ERROR expected identifier
44
cfg!(foo = 123); //~ ERROR literal in `cfg` predicate value must be a string
5+
cfg!(foo, bar); //~ ERROR expected 1 cfg-pattern
56
}

‎src/test/ui/macros/cfg.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ error[E0565]: literal in `cfg` predicate value must be a string
1818
LL | cfg!(foo = 123);
1919
| ^^^
2020

21-
error: aborting due to 3 previous errors
21+
error: expected 1 cfg-pattern
22+
--> $DIR/cfg.rs:5:5
23+
|
24+
LL | cfg!(foo, bar);
25+
| ^^^^^^^^^^^^^^
26+
|
27+
= note: this error originates in the macro `cfg` (in Nightly builds, run with -Z macro-backtrace for more info)
28+
29+
error: aborting due to 4 previous errors
2230

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

0 commit comments

Comments
 (0)
Please sign in to comment.