Skip to content

Commit 83a724e

Browse files
committed
Refactor more diagnostics in rustc_attr
1 parent 0005f62 commit 83a724e

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ where
237237
let mut promotable = false;
238238
let mut allowed_through_unstable_modules = false;
239239

240-
let diagnostic = &sess.parse_sess.span_diagnostic;
241-
242240
'outer: for attr in attrs_iter {
243241
if ![
244242
sym::rustc_const_unstable,
@@ -278,7 +276,7 @@ where
278276
*item = Some(v);
279277
true
280278
} else {
281-
struct_span_err!(diagnostic, meta.span, E0539, "incorrect meta item").emit();
279+
sess.emit_err(session_diagnostics::InvalidMetaItem { span: meta.span });
282280
false
283281
}
284282
};
@@ -344,39 +342,28 @@ where
344342
// is a name/value pair string literal.
345343
issue_num = match issue.unwrap().as_str() {
346344
"none" => None,
347-
issue => {
348-
let emit_diag = |msg: &str| {
349-
struct_span_err!(
350-
diagnostic,
351-
mi.span,
352-
E0545,
353-
"`issue` must be a non-zero numeric string \
354-
or \"none\"",
355-
)
356-
.span_label(mi.name_value_literal_span().unwrap(), msg)
357-
.emit();
358-
};
359-
match issue.parse() {
360-
Ok(0) => {
361-
emit_diag(
362-
"`issue` must not be \"0\", \
363-
use \"none\" instead",
364-
);
365-
continue 'outer;
366-
}
367-
Ok(num) => NonZeroU32::new(num),
368-
Err(err) => {
369-
emit_diag(&err.to_string());
370-
continue 'outer;
371-
}
345+
issue => match issue.parse::<NonZeroU32>() {
346+
Ok(num) => Some(num),
347+
Err(err) => {
348+
sess.emit_err(
349+
session_diagnostics::InvalidIssueString {
350+
span: mi.span,
351+
cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
352+
mi.name_value_literal_span().unwrap(),
353+
err.kind(),
354+
),
355+
},
356+
);
357+
continue 'outer;
372358
}
373-
}
359+
},
374360
};
375361
}
376362
sym::soft => {
377363
if !mi.is_word() {
378-
let msg = "`soft` should not have any arguments";
379-
sess.parse_sess.span_diagnostic.span_err(mi.span, msg);
364+
sess.emit_err(session_diagnostics::SoftNoArgs {
365+
span: mi.span,
366+
});
380367
}
381368
is_soft = true;
382369
}
@@ -434,8 +421,7 @@ where
434421
continue;
435422
}
436423
_ => {
437-
struct_span_err!(diagnostic, attr.span, E0547, "missing 'issue'")
438-
.emit();
424+
sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
439425
continue;
440426
}
441427
}
@@ -530,14 +516,7 @@ where
530516
if let Some((ref mut stab, _)) = const_stab {
531517
stab.promotable = promotable;
532518
} else {
533-
struct_span_err!(
534-
diagnostic,
535-
item_sp,
536-
E0717,
537-
"`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
538-
or a `rustc_const_stable` attribute"
539-
)
540-
.emit();
519+
sess.emit_err(session_diagnostics::RustcPromotablePairing { span: item_sp });
541520
}
542521
}
543522

@@ -552,13 +531,7 @@ where
552531
{
553532
*allowed_through_unstable_modules = true;
554533
} else {
555-
struct_span_err!(
556-
diagnostic,
557-
item_sp,
558-
E0789,
559-
"`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute"
560-
)
561-
.emit();
534+
sess.emit_err(session_diagnostics::RustcAllowedUnstablePairing { span: item_sp });
562535
}
563536
}
564537

compiler/rustc_error_messages/locales/en-US/attr.ftl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,31 @@ attr_unsupported_literal_deprecated_kv_pair =
2727
item in `deprecated` must be a key/value pair
2828
attr_unsupported_literal_suggestion =
2929
consider removing the prefix
30+
31+
attr_invalid_meta_item =
32+
incorrect meta item
33+
34+
attr_invalid_issue_string =
35+
`issue` must be a non-zero numeric string or "none"
36+
attr_must_not_be_zero =
37+
`issue` must not be "0", use "none" instead
38+
attr_empty =
39+
cannot parse integer from empty string
40+
attr_invalid_digit =
41+
invalid digit found in string
42+
attr_pos_overflow =
43+
number too large to fit in target type
44+
attr_neg_overflow =
45+
number too small to fit in target type
46+
47+
attr_missing_issue =
48+
missing 'issue'
49+
50+
attr_rustc_promotable_pairing =
51+
`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` or a `rustc_const_stable` attribute
52+
53+
attr_rustc_allowed_unstable_pairing =
54+
`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute
55+
56+
attr_soft_no_args =
57+
`soft` should not have any arguments

0 commit comments

Comments
 (0)