Skip to content

Commit a6cd4aa

Browse files
committed
Auto merge of #93386 - WaffleLapkin:rustc_must_implement_one_of_check_target, r=nagisa
Check that `#[rustc_must_implement_one_of]` is applied to a trait `#[rustc_must_implement_one_of]` only makes sense when applied to a trait, so it's sensible to emit an error otherwise.
2 parents 745e926 + 4ca56d2 commit a6cd4aa

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ impl CheckAttrVisitor<'_> {
7676
sym::inline => self.check_inline(hir_id, attr, span, target),
7777
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
7878
sym::marker => self.check_marker(hir_id, attr, span, target),
79+
sym::rustc_must_implement_one_of => {
80+
self.check_rustc_must_implement_one_of(attr, span, target)
81+
}
7982
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
8083
sym::track_caller => {
8184
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
@@ -477,6 +480,26 @@ impl CheckAttrVisitor<'_> {
477480
}
478481
}
479482

483+
/// Checks if the `#[rustc_must_implement_one_of]` attribute on a `target` is valid. Returns `true` if valid.
484+
fn check_rustc_must_implement_one_of(
485+
&self,
486+
attr: &Attribute,
487+
span: &Span,
488+
target: Target,
489+
) -> bool {
490+
match target {
491+
Target::Trait => true,
492+
_ => {
493+
self.tcx
494+
.sess
495+
.struct_span_err(attr.span, "attribute can only be applied to a trait")
496+
.span_label(*span, "not a trait")
497+
.emit();
498+
false
499+
}
500+
}
501+
}
502+
480503
/// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
481504
fn check_target_feature(
482505
&self,

src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs

+8
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ trait Tr5 {
3535
fn b(); //~ This function doesn't have a default implementation
3636
}
3737

38+
#[rustc_must_implement_one_of(abc, xyz)]
39+
//~^ attribute can only be applied to a trait
40+
fn function() {}
41+
42+
#[rustc_must_implement_one_of(abc, xyz)]
43+
//~^ attribute can only be applied to a trait
44+
struct Struct {}
45+
3846
fn main() {}

src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ error: malformed `rustc_must_implement_one_of` attribute input
44
LL | #[rustc_must_implement_one_of]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_must_implement_one_of(function1, function2, ...)]`
66

7+
error: attribute can only be applied to a trait
8+
--> $DIR/rustc_must_implement_one_of_misuse.rs:38:1
9+
|
10+
LL | #[rustc_must_implement_one_of(abc, xyz)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL |
13+
LL | fn function() {}
14+
| ---------------- not a trait
15+
16+
error: attribute can only be applied to a trait
17+
--> $DIR/rustc_must_implement_one_of_misuse.rs:42:1
18+
|
19+
LL | #[rustc_must_implement_one_of(abc, xyz)]
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
LL |
22+
LL | struct Struct {}
23+
| ---------------- not a trait
24+
725
error: Function not found in this trait
826
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
927
|
@@ -78,5 +96,5 @@ note: required by this annotation
7896
LL | #[rustc_must_implement_one_of(a, b)]
7997
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8098

81-
error: aborting due to 9 previous errors
99+
error: aborting due to 11 previous errors
82100

0 commit comments

Comments
 (0)