Skip to content

Commit f7ca6d3

Browse files
committed
Auto merge of #155174 - mejrs:on_move_gating, r=<try>
Improve emission of `UnknownDiagnosticAttribute` lint
2 parents bf4fbfb + 55cd476 commit f7ca6d3

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<S: Stage> AttributeParser<S> for OnConstParser {
1717
template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
1818
|this, cx, args| {
1919
if !cx.features().diagnostic_on_const() {
20+
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
2021
return;
2122
}
2223

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl OnMoveParser {
2424
mode: Mode,
2525
) {
2626
if !cx.features().diagnostic_on_move() {
27+
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
2728
return;
2829
}
2930

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl OnUnknownParser {
1818
mode: Mode,
1919
) {
2020
if !cx.features().diagnostic_on_unknown() {
21+
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
2122
return;
2223
}
2324
let span = cx.attr_span;

compiler/rustc_resolve/src/macros.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -712,27 +712,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
712712
feature_err(&self.tcx.sess, sym::custom_inner_attributes, path.span, msg).emit();
713713
}
714714

715-
let diagnostic_attributes: &[(Symbol, bool)] = &[
716-
(sym::on_unimplemented, true),
717-
(sym::do_not_recommend, true),
718-
(sym::on_move, true),
719-
(sym::on_const, self.tcx.features().diagnostic_on_const()),
720-
(sym::on_unknown, self.tcx.features().diagnostic_on_unknown()),
715+
const DIAGNOSTIC_ATTRIBUTES: &[(Symbol, Option<Symbol>)] = &[
716+
(sym::on_unimplemented, None),
717+
(sym::do_not_recommend, None),
718+
(sym::on_move, Some(sym::diagnostic_on_move)),
719+
(sym::on_const, Some(sym::diagnostic_on_const)),
720+
(sym::on_unknown, Some(sym::diagnostic_on_unknown)),
721721
];
722722

723723
if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)
724724
&& let [namespace, attribute, ..] = &*path.segments
725725
&& namespace.ident.name == sym::diagnostic
726-
&& !diagnostic_attributes
727-
.iter()
728-
.any(|(attr, stable)| *stable && attribute.ident.name == *attr)
726+
&& !DIAGNOSTIC_ATTRIBUTES.iter().any(|(attr, stable)| {
727+
attribute.ident.name == *attr
728+
&& stable.is_none_or(|f| self.tcx.features().enabled(f))
729+
})
729730
{
730731
let span = attribute.span();
731-
let candidates = diagnostic_attributes
732+
let candidates = DIAGNOSTIC_ATTRIBUTES
732733
.iter()
733-
.filter_map(|(sym, stable)| stable.then_some(*sym))
734+
.filter_map(|(sym, stable)| {
735+
stable.is_none_or(|f| self.tcx.features().enabled(f)).then_some(*sym)
736+
})
734737
.collect::<Vec<_>>();
735-
let typo = find_best_match_for_name(&candidates, attribute.ident.name, Some(5))
738+
let typo = find_best_match_for_name(&candidates, attribute.ident.name, None)
736739
.map(|typo_name| errors::UnknownDiagnosticAttributeTypoSugg { span, typo_name });
737740

738741
self.tcx.sess.psess.buffer_lint(

tests/ui/feature-gates/feature-gate-diagnostic-on-move.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
//! gate, but the fact that not adding the feature gate will cause the
33
//! diagnostic to not emit the custom diagnostic message
44
//!
5-
#[diagnostic::on_move(
6-
message = "Foo"
7-
)]
5+
#[diagnostic::on_move(message = "Foo")]
6+
//~^ WARN unknown diagnostic attribute
87
#[derive(Debug)]
98
struct Foo;
109

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
warning: unknown diagnostic attribute
2+
--> $DIR/feature-gate-diagnostic-on-move.rs:5:15
3+
|
4+
LL | #[diagnostic::on_move(message = "Foo")]
5+
| ^^^^^^^
6+
|
7+
= note: `#[warn(unknown_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
8+
19
error[E0382]: use of moved value: `foo`
2-
--> $DIR/feature-gate-diagnostic-on-move.rs:16:15
10+
--> $DIR/feature-gate-diagnostic-on-move.rs:15:15
311
|
412
LL | let foo = Foo;
513
| --- move occurs because `foo` has type `Foo`, which does not implement the `Copy` trait
@@ -9,21 +17,21 @@ LL | let bar = foo;
917
| ^^^ value used here after move
1018
|
1119
note: consider changing this parameter type in function `takes_foo` to borrow instead if owning the value isn't necessary
12-
--> $DIR/feature-gate-diagnostic-on-move.rs:11:17
20+
--> $DIR/feature-gate-diagnostic-on-move.rs:10:17
1321
|
1422
LL | fn takes_foo(_: Foo) {}
1523
| --------- ^^^ this parameter takes ownership of the value
1624
| |
1725
| in this function
1826
note: if `Foo` implemented `Clone`, you could clone the value
19-
--> $DIR/feature-gate-diagnostic-on-move.rs:9:1
27+
--> $DIR/feature-gate-diagnostic-on-move.rs:8:1
2028
|
2129
LL | struct Foo;
2230
| ^^^^^^^^^^ consider implementing `Clone` for this type
2331
...
2432
LL | takes_foo(foo);
2533
| --- you could clone this value
2634

27-
error: aborting due to 1 previous error
35+
error: aborting due to 1 previous error; 1 warning emitted
2836

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

0 commit comments

Comments
 (0)