Skip to content

Commit 032cbe4

Browse files
committed
Check if the attribute is applied correctly
1 parent 56d79ad commit 032cbe4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ impl CheckAttrVisitor<'tcx> {
9898
| sym::rustc_if_this_changed
9999
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
100100
sym::cmse_nonsecure_entry => self.check_cmse_nonsecure_entry(attr, span, target),
101+
sym::default_method_body_is_const => {
102+
self.check_default_method_body_is_const(attr, span, target)
103+
}
101104
_ => true,
102105
};
103106
// lint-only checks
@@ -1465,6 +1468,29 @@ impl CheckAttrVisitor<'tcx> {
14651468
}
14661469
}
14671470
}
1471+
1472+
/// default_method_body_is_const should only be applied to trait methods with default bodies.
1473+
fn check_default_method_body_is_const(
1474+
&self,
1475+
attr: &Attribute,
1476+
span: &Span,
1477+
target: Target,
1478+
) -> bool {
1479+
match target {
1480+
Target::Method(MethodKind::Trait { body: true }) => true,
1481+
_ => {
1482+
self.tcx
1483+
.sess
1484+
.struct_span_err(
1485+
attr.span,
1486+
"attribute should be applied to a trait method with body",
1487+
)
1488+
.span_label(*span, "not a trait method or missing a body")
1489+
.emit();
1490+
false
1491+
}
1492+
}
1493+
}
14681494
}
14691495

14701496
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {

0 commit comments

Comments
 (0)