Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that #[rustc_must_implement_one_of] is applied to a trait #93386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ impl CheckAttrVisitor<'_> {
sym::inline => self.check_inline(hir_id, attr, span, target),
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
sym::marker => self.check_marker(hir_id, attr, span, target),
sym::rustc_must_implement_one_of => {
self.check_rustc_must_implement_one_of(attr, span, target)
}
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
sym::track_caller => {
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
Expand Down Expand Up @@ -477,6 +480,26 @@ impl CheckAttrVisitor<'_> {
}
}

/// Checks if the `#[rustc_must_implement_one_of]` attribute on a `target` is valid. Returns `true` if valid.
fn check_rustc_must_implement_one_of(
&self,
attr: &Attribute,
span: &Span,
target: Target,
) -> bool {
match target {
Target::Trait => true,
_ => {
self.tcx
.sess
.struct_span_err(attr.span, "attribute can only be applied to a trait")
.span_label(*span, "not a trait")
.emit();
false
}
}
}

/// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
fn check_target_feature(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ trait Tr5 {
fn b(); //~ This function doesn't have a default implementation
}

#[rustc_must_implement_one_of(abc, xyz)]
//~^ attribute can only be applied to a trait
fn function() {}

#[rustc_must_implement_one_of(abc, xyz)]
//~^ attribute can only be applied to a trait
struct Struct {}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ error: malformed `rustc_must_implement_one_of` attribute input
LL | #[rustc_must_implement_one_of]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_must_implement_one_of(function1, function2, ...)]`

error: attribute can only be applied to a trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:38:1
|
LL | #[rustc_must_implement_one_of(abc, xyz)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | fn function() {}
| ---------------- not a trait

error: attribute can only be applied to a trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:42:1
|
LL | #[rustc_must_implement_one_of(abc, xyz)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | struct Struct {}
| ---------------- not a trait

error: Function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
|
Expand Down Expand Up @@ -78,5 +96,5 @@ note: required by this annotation
LL | #[rustc_must_implement_one_of(a, b)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: aborting due to 11 previous errors