Skip to content

Commit a5d0719

Browse files
Clarify and make consistent the feature gating of on_unknown_attr
1 parent b3cddc7 commit a5d0719

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_unknown.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ impl OnUnknownParser {
1616
args: &ArgParser,
1717
mode: Mode,
1818
) {
19-
if !cx.features().diagnostic_on_unknown() {
19+
if let Some(features) = cx.features
20+
&& !features.diagnostic_on_unknown()
21+
{
2022
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
2123
return;
2224
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,13 @@ pub(crate) struct OnUnknownData {
149149

150150
impl OnUnknownData {
151151
pub(crate) fn from_attrs<'tcx>(tcx: TyCtxt<'tcx>, item: &Item) -> Option<OnUnknownData> {
152-
if let Some(Attribute::Parsed(AttributeKind::OnUnknown { directive, .. })) =
153-
AttributeParser::parse_limited(
154-
tcx.sess,
155-
&item.attrs,
156-
&[sym::diagnostic, sym::on_unknown],
157-
item.span,
158-
item.id,
159-
Some(tcx.features()),
160-
)
152+
if tcx.features().diagnostic_on_unknown()
153+
&& let Some(Attribute::Parsed(AttributeKind::OnUnknown { directive, .. })) =
154+
AttributeParser::parse_limited(
155+
tcx.sess,
156+
&item.attrs,
157+
&[sym::diagnostic, sym::on_unknown],
158+
)
161159
{
162160
Some(Self { directive: Box::new(*directive?) })
163161
} else {
@@ -216,6 +214,8 @@ pub(crate) struct ImportData<'ra> {
216214
/// A `#[diagnostic::on_unknown]` attribute applied
217215
/// to the given import. This allows crates to specify
218216
/// custom error messages for a specific import
217+
///
218+
/// This is `None` if the feature flag for `diagnostic::on_unknown` is disabled.
219219
pub on_unknown_attr: Option<OnUnknownData>,
220220
}
221221

@@ -845,24 +845,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
845845
.collect::<Vec<_>>();
846846
let default_message =
847847
format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),);
848-
let (message, label, notes) = if self.tcx.features().diagnostic_on_unknown()
849-
&& let Some(directive) = errors[0].1.on_unknown_attr.as_ref().map(|a| &a.directive)
850-
{
851-
let args = FormatArgs {
852-
this: paths.join(", "),
853-
// Unused
854-
this_sugared: String::new(),
855-
// Unused
856-
item_context: "",
857-
// Unused
858-
generic_args: Vec::new(),
859-
};
860-
let CustomDiagnostic { message, label, notes, .. } = directive.eval(None, &args);
848+
let (message, label, notes) =
849+
// Feature gating for `on_unknown_attr` happens initialization of the field
850+
if let Some(directive) = errors[0].1.on_unknown_attr.as_ref().map(|a| &a.directive) {
851+
let args = FormatArgs {
852+
this: paths.join(", "),
853+
// Unused
854+
this_sugared: String::new(),
855+
// Unused
856+
item_context: "",
857+
// Unused
858+
generic_args: Vec::new(),
859+
};
860+
let CustomDiagnostic { message, label, notes, .. } = directive.eval(None, &args);
861861

862-
(message, label, notes)
863-
} else {
864-
(None, None, Vec::new())
865-
};
862+
(message, label, notes)
863+
} else {
864+
(None, None, Vec::new())
865+
};
866866
let has_custom_message = message.is_some();
867867
let message = message.as_deref().unwrap_or(default_message.as_str());
868868

0 commit comments

Comments
 (0)