Skip to content

Commit 2c5fdb9

Browse files
Rollup merge of #158138 - obeis:move-check-loop-match, r=JonathanBrouwer
Remove redundant check for `#[loop_match]` and `#[const_continue]` Updates #153101 remove `check_loop_match` and `check_const_continue` from `check_attr.rs` as they only verifies that the target, which is already enforced by `check_target` in `target_checking.rs` via `ALLOWED_TARGETS`. r? @JonathanBrouwer
2 parents 0c1cae8 + 620bf09 commit 2c5fdb9

7 files changed

Lines changed: 26 additions & 74 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/loop_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl NoArgsAttributeParser for LoopMatchParser {
1313
pub(crate) struct ConstContinueParser;
1414
impl NoArgsAttributeParser for ConstContinueParser {
1515
const PATH: &[Symbol] = &[sym::const_continue];
16-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Expression)]);
16+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Break)]);
1717
const STABILITY: AttributeStability = unstable!(loop_match);
1818
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ConstContinue;
1919
}

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,5 +527,6 @@ pub(crate) const ALL_TARGETS: &'static [Policy] = {
527527
Allow(Target::Loop),
528528
Allow(Target::ForLoop),
529529
Allow(Target::While),
530+
Allow(Target::Break),
530531
]
531532
};

compiler/rustc_hir/src/target.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub enum Target {
7070
ForLoop,
7171
While,
7272
Loop,
73+
Break,
7374
}
7475

7576
impl Display for Target {
@@ -119,7 +120,8 @@ impl Target {
119120
| Target::Delegation { .. }
120121
| Target::Loop
121122
| Target::While
122-
| Target::ForLoop => false,
123+
| Target::ForLoop
124+
| Target::Break => false,
123125
}
124126
}
125127

@@ -265,6 +267,7 @@ impl Target {
265267
ast::ExprKind::ForLoop { .. } => Self::ForLoop,
266268
ast::ExprKind::Loop(..) => Self::Loop,
267269
ast::ExprKind::While(..) => Self::While,
270+
ast::ExprKind::Break(..) => Self::Break,
268271
_ => Self::Expression,
269272
}
270273
}
@@ -319,6 +322,7 @@ impl Target {
319322
Target::Loop => "loop",
320323
Target::ForLoop => "for loop",
321324
Target::While => "while loop",
325+
Target::Break => "break expression",
322326
}
323327
}
324328

@@ -373,6 +377,7 @@ impl Target {
373377
Target::ForLoop => "for loops",
374378
Target::Loop => "loops",
375379
Target::While => "while loops",
380+
Target::Break => "break expressions",
376381
}
377382
}
378383
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
193193
AttributeKind::Inline(kind, attr_span) => {
194194
self.check_inline(hir_id, *attr_span, kind, target)
195195
}
196-
AttributeKind::LoopMatch(attr_span) => {
197-
self.check_loop_match(hir_id, *attr_span, target)
198-
}
199-
AttributeKind::ConstContinue(attr_span) => {
200-
self.check_const_continue(hir_id, *attr_span, target)
201-
}
202196
AttributeKind::AllowInternalUnsafe(attr_span)
203197
| AttributeKind::AllowInternalUnstable(.., attr_span) => {
204198
self.check_macro_only_attr(*attr_span, span, target, attrs)
@@ -218,7 +212,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
218212
&AttributeKind::RustcPubTransparent(attr_span) => {
219213
self.check_rustc_pub_transparent(attr_span, span, attrs)
220214
}
221-
AttributeKind::RustcAlign { .. } => {}
222215
AttributeKind::Naked(..) => self.check_naked(hir_id, target),
223216
AttributeKind::TrackCaller(attr_span) => {
224217
self.check_track_caller(hir_id, *attr_span, attrs, target)
@@ -262,6 +255,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
262255
AttributeKind::Cold => (),
263256
AttributeKind::CollapseDebugInfo(..) => (),
264257
AttributeKind::CompilerBuiltins => (),
258+
AttributeKind::ConstContinue(..) => {}
265259
AttributeKind::Coroutine => (),
266260
AttributeKind::Coverage(..) => (),
267261
AttributeKind::CrateName { .. } => (),
@@ -286,6 +280,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
286280
AttributeKind::LinkOrdinal { .. } => (),
287281
AttributeKind::LinkSection { .. } => (),
288282
AttributeKind::Linkage(..) => (),
283+
AttributeKind::LoopMatch(..) => {}
289284
AttributeKind::MacroEscape => (),
290285
AttributeKind::MacroUse { .. } => (),
291286
AttributeKind::Marker => (),
@@ -317,6 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
317312
// handled below this loop and elsewhere
318313
AttributeKind::Repr { .. } => (),
319314
AttributeKind::RustcAbi { .. } => (),
315+
AttributeKind::RustcAlign { .. } => {}
320316
AttributeKind::RustcAllocator => (),
321317
AttributeKind::RustcAllocatorZeroed => (),
322318
AttributeKind::RustcAllocatorZeroedVariant { .. } => (),
@@ -891,7 +887,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
891887
| Target::Delegation { .. }
892888
| Target::Loop
893889
| Target::ForLoop
894-
| Target::While => None,
890+
| Target::While
891+
| Target::Break => None,
895892
} {
896893
self.tcx.dcx().emit_err(diagnostics::DocAliasBadLocation { span, location });
897894
return;
@@ -1662,30 +1659,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16621659
.emit_err(diagnostics::BothOptimizeNoneAndInline { optimize_span, inline_span });
16631660
}
16641661
}
1665-
1666-
fn check_loop_match(&self, hir_id: HirId, attr_span: Span, target: Target) {
1667-
let node_span = self.tcx.hir_span(hir_id);
1668-
1669-
if !matches!(target, Target::Expression) {
1670-
return; // Handled in target checking during attr parse
1671-
}
1672-
1673-
if !matches!(self.tcx.hir_expect_expr(hir_id).kind, hir::ExprKind::Loop(..)) {
1674-
self.dcx().emit_err(diagnostics::LoopMatchAttr { attr_span, node_span });
1675-
};
1676-
}
1677-
1678-
fn check_const_continue(&self, hir_id: HirId, attr_span: Span, target: Target) {
1679-
let node_span = self.tcx.hir_span(hir_id);
1680-
1681-
if !matches!(target, Target::Expression) {
1682-
return; // Handled in target checking during attr parse
1683-
}
1684-
1685-
if !matches!(self.tcx.hir_expect_expr(hir_id).kind, hir::ExprKind::Break(..)) {
1686-
self.dcx().emit_err(diagnostics::ConstContinueAttr { attr_span, node_span });
1687-
};
1688-
}
16891662
}
16901663

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

compiler/rustc_passes/src/diagnostics.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
1313
use crate::check_attr::ProcMacroKind;
1414
use crate::lang_items::Duplicate;
1515

16-
#[derive(Diagnostic)]
17-
#[diag("`#[loop_match]` should be applied to a loop")]
18-
pub(crate) struct LoopMatchAttr {
19-
#[primary_span]
20-
pub attr_span: Span,
21-
#[label("not a loop")]
22-
pub node_span: Span,
23-
}
24-
25-
#[derive(Diagnostic)]
26-
#[diag("`#[const_continue]` should be applied to a break expression")]
27-
pub(crate) struct ConstContinueAttr {
28-
#[primary_span]
29-
pub attr_span: Span,
30-
#[label("not a break expression")]
31-
pub node_span: Span,
32-
}
33-
3416
#[derive(Diagnostic)]
3517
#[diag("`{$no_mangle_attr}` attribute may not be used in combination with `{$export_name_attr}`")]
3618
pub(crate) struct MixedExportNameAndNoMangle {

tests/ui/loop-match/invalid-attribute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn main() {
3737

3838
{
3939
#[loop_match] //~ ERROR attribute cannot be used on
40-
//~^ ERROR `#[loop_match]` should be applied to a loop
41-
#[const_continue] //~ ERROR should be applied to a break expression
40+
#[const_continue]
41+
//~^ ERROR `#[const_continue]` attribute cannot be used on expressions
4242
5
4343
};
4444
}

tests/ui/loop-match/invalid-attribute.stderr

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: `#[const_continue]` attribute cannot be used on crates
1212
LL | #![const_continue]
1313
| ^^^^^^^^^^^^^^^^^^
1414
|
15-
= help: `#[const_continue]` can be applied to
15+
= help: `#[const_continue]` can only be applied to break expressions
1616

1717
error: `#[loop_match]` attribute cannot be used on foreign functions
1818
--> $DIR/invalid-attribute.rs:10:5
@@ -28,7 +28,7 @@ error: `#[const_continue]` attribute cannot be used on foreign functions
2828
LL | #[const_continue]
2929
| ^^^^^^^^^^^^^^^^^
3030
|
31-
= help: `#[const_continue]` can be applied to
31+
= help: `#[const_continue]` can only be applied to break expressions
3232

3333
error: `#[loop_match]` attribute cannot be used on structs
3434
--> $DIR/invalid-attribute.rs:15:1
@@ -44,7 +44,7 @@ error: `#[const_continue]` attribute cannot be used on structs
4444
LL | #[const_continue]
4545
| ^^^^^^^^^^^^^^^^^
4646
|
47-
= help: `#[const_continue]` can be applied to
47+
= help: `#[const_continue]` can only be applied to break expressions
4848

4949
error: `#[loop_match]` attribute cannot be used on required trait methods
5050
--> $DIR/invalid-attribute.rs:24:5
@@ -60,7 +60,7 @@ error: `#[const_continue]` attribute cannot be used on required trait methods
6060
LL | #[const_continue]
6161
| ^^^^^^^^^^^^^^^^^
6262
|
63-
= help: `#[const_continue]` can be applied to
63+
= help: `#[const_continue]` can only be applied to break expressions
6464

6565
error: `#[loop_match]` attribute cannot be used on functions
6666
--> $DIR/invalid-attribute.rs:29:1
@@ -76,7 +76,7 @@ error: `#[const_continue]` attribute cannot be used on functions
7676
LL | #[const_continue]
7777
| ^^^^^^^^^^^^^^^^^
7878
|
79-
= help: `#[const_continue]` can be applied to
79+
= help: `#[const_continue]` can only be applied to break expressions
8080

8181
error: `#[loop_match]` attribute cannot be used on closures
8282
--> $DIR/invalid-attribute.rs:34:5
@@ -92,7 +92,7 @@ error: `#[const_continue]` attribute cannot be used on closures
9292
LL | #[const_continue]
9393
| ^^^^^^^^^^^^^^^^^
9494
|
95-
= help: `#[const_continue]` can be applied to
95+
= help: `#[const_continue]` can only be applied to break expressions
9696

9797
error: `#[loop_match]` attribute cannot be used on expressions
9898
--> $DIR/invalid-attribute.rs:39:9
@@ -102,22 +102,13 @@ LL | #[loop_match]
102102
|
103103
= help: `#[loop_match]` can only be applied to loops
104104

105-
error: `#[loop_match]` should be applied to a loop
106-
--> $DIR/invalid-attribute.rs:39:9
107-
|
108-
LL | #[loop_match]
109-
| ^^^^^^^^^^^^^
110-
...
111-
LL | 5
112-
| - not a loop
113-
114-
error: `#[const_continue]` should be applied to a break expression
115-
--> $DIR/invalid-attribute.rs:41:9
105+
error: `#[const_continue]` attribute cannot be used on expressions
106+
--> $DIR/invalid-attribute.rs:40:9
116107
|
117108
LL | #[const_continue]
118109
| ^^^^^^^^^^^^^^^^^
119-
LL | 5
120-
| - not a break expression
110+
|
111+
= help: `#[const_continue]` can only be applied to break expressions
121112

122-
error: aborting due to 15 previous errors
113+
error: aborting due to 14 previous errors
123114

0 commit comments

Comments
 (0)