Skip to content

Commit f6f362c

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#106716 - c410-f3r:rfc-2397-1, r=davidtwco
[RFC 2397] Deny incorrect locations cc rust-lang#51992 As declared in the RFC, `#[do_not_recommend]` should only be applicable on trait implementations.
2 parents 9a2f393 + 7dd45ba commit f6f362c

File tree

7 files changed

+121
-2
lines changed

7 files changed

+121
-2
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
-passes_see_issue =
55
see issue #{$issue} <https://github.com/rust-lang/rust/issues/{$issue}> for more information
66
7+
passes_incorrect_do_not_recommend_location =
8+
`#[do_not_recommend]` can only be placed on trait implementations
9+
710
passes_outer_crate_level_attr =
811
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
912

compiler/rustc_passes/src/check_attr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ impl CheckAttrVisitor<'_> {
8282
let attrs = self.tcx.hir().attrs(hir_id);
8383
for attr in attrs {
8484
let attr_is_valid = match attr.name_or_empty() {
85+
sym::do_not_recommend => self.check_do_not_recommend(attr.span, target),
8586
sym::inline => self.check_inline(hir_id, attr, span, target),
8687
sym::no_coverage => self.check_no_coverage(hir_id, attr, span, target),
8788
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
@@ -241,6 +242,16 @@ impl CheckAttrVisitor<'_> {
241242
);
242243
}
243244

245+
/// Checks if `#[do_not_recommend]` is applied on a trait impl.
246+
fn check_do_not_recommend(&self, attr_span: Span, target: Target) -> bool {
247+
if let Target::Impl = target {
248+
true
249+
} else {
250+
self.tcx.sess.emit_err(errors::IncorrectDoNotRecommendLocation { span: attr_span });
251+
false
252+
}
253+
}
254+
244255
/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
245256
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
246257
match target {

compiler/rustc_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ use rustc_span::{Span, Symbol, DUMMY_SP};
1414

1515
use crate::lang_items::Duplicate;
1616

17+
#[derive(Diagnostic)]
18+
#[diag(passes_incorrect_do_not_recommend_location)]
19+
pub struct IncorrectDoNotRecommendLocation {
20+
#[primary_span]
21+
pub span: Span,
22+
}
23+
1724
#[derive(LintDiagnostic)]
1825
#[diag(passes_outer_crate_level_attr)]
1926
pub struct OuterCrateLevelAttr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![feature(do_not_recommend)]
2+
3+
#[do_not_recommend]
4+
//~^ `#[do_not_recommend]` can only be placed
5+
const CONST: () = ();
6+
7+
#[do_not_recommend]
8+
//~^ `#[do_not_recommend]` can only be placed
9+
static Static: () = ();
10+
11+
#[do_not_recommend]
12+
//~^ `#[do_not_recommend]` can only be placed
13+
type Type = ();
14+
15+
#[do_not_recommend]
16+
//~^ `#[do_not_recommend]` can only be placed
17+
enum Enum {
18+
}
19+
20+
#[do_not_recommend]
21+
//~^ `#[do_not_recommend]` can only be placed
22+
extern {
23+
}
24+
25+
#[do_not_recommend]
26+
//~^ `#[do_not_recommend]` can only be placed
27+
fn fun() {
28+
}
29+
30+
#[do_not_recommend]
31+
//~^ `#[do_not_recommend]` can only be placed
32+
struct Struct {
33+
}
34+
35+
#[do_not_recommend]
36+
//~^ `#[do_not_recommend]` can only be placed
37+
trait Trait {
38+
}
39+
40+
#[do_not_recommend]
41+
impl Trait for i32 {
42+
}
43+
44+
fn main() {
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: `#[do_not_recommend]` can only be placed on trait implementations
2+
--> $DIR/incorrect-locations.rs:3:1
3+
|
4+
LL | #[do_not_recommend]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
7+
error: `#[do_not_recommend]` can only be placed on trait implementations
8+
--> $DIR/incorrect-locations.rs:7:1
9+
|
10+
LL | #[do_not_recommend]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: `#[do_not_recommend]` can only be placed on trait implementations
14+
--> $DIR/incorrect-locations.rs:11:1
15+
|
16+
LL | #[do_not_recommend]
17+
| ^^^^^^^^^^^^^^^^^^^
18+
19+
error: `#[do_not_recommend]` can only be placed on trait implementations
20+
--> $DIR/incorrect-locations.rs:15:1
21+
|
22+
LL | #[do_not_recommend]
23+
| ^^^^^^^^^^^^^^^^^^^
24+
25+
error: `#[do_not_recommend]` can only be placed on trait implementations
26+
--> $DIR/incorrect-locations.rs:20:1
27+
|
28+
LL | #[do_not_recommend]
29+
| ^^^^^^^^^^^^^^^^^^^
30+
31+
error: `#[do_not_recommend]` can only be placed on trait implementations
32+
--> $DIR/incorrect-locations.rs:25:1
33+
|
34+
LL | #[do_not_recommend]
35+
| ^^^^^^^^^^^^^^^^^^^
36+
37+
error: `#[do_not_recommend]` can only be placed on trait implementations
38+
--> $DIR/incorrect-locations.rs:30:1
39+
|
40+
LL | #[do_not_recommend]
41+
| ^^^^^^^^^^^^^^^^^^^
42+
43+
error: `#[do_not_recommend]` can only be placed on trait implementations
44+
--> $DIR/incorrect-locations.rs:35:1
45+
|
46+
LL | #[do_not_recommend]
47+
| ^^^^^^^^^^^^^^^^^^^
48+
49+
error: aborting due to 8 previous errors
50+

tests/ui/rfc-2397-do-not-recommend/unstable-feature.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
trait Foo {
2+
}
3+
14
#[do_not_recommend]
25
//~^ ERROR the `#[do_not_recommend]` attribute is an experimental feature
3-
trait Foo {
6+
impl Foo for i32 {
47
}
58

69
fn main() {

tests/ui/rfc-2397-do-not-recommend/unstable-feature.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: the `#[do_not_recommend]` attribute is an experimental feature
2-
--> $DIR/unstable-feature.rs:1:1
2+
--> $DIR/unstable-feature.rs:4:1
33
|
44
LL | #[do_not_recommend]
55
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)