Skip to content

Commit ff0096d

Browse files
HIR const-checking
1 parent 23a907e commit ff0096d

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

src/librustc_passes/check_const.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ use rustc_span::{sym, Span, Symbol};
2323
enum NonConstExpr {
2424
Loop(hir::LoopSource),
2525
Match(hir::MatchSource),
26-
OrPattern,
2726
}
2827

2928
impl NonConstExpr {
3029
fn name(self) -> String {
3130
match self {
3231
Self::Loop(src) => format!("`{}`", src.name()),
3332
Self::Match(src) => format!("`{}`", src.name()),
34-
Self::OrPattern => "or-pattern".to_string(),
3533
}
3634
}
3735

@@ -40,22 +38,19 @@ impl NonConstExpr {
4038
use hir::MatchSource::*;
4139

4240
let gates: &[_] = match self {
43-
Self::Match(Normal)
44-
| Self::Match(IfDesugar { .. })
45-
| Self::Match(IfLetDesugar { .. })
46-
| Self::OrPattern => &[sym::const_if_match],
47-
48-
Self::Loop(Loop) => &[sym::const_loop],
41+
// A `for` loop's desugaring contains a call to `IntoIterator::into_iter`,
42+
// so they are not yet allowed with `#![feature(const_loop)]`.
43+
// Likewise, `?` desugars to a call to `Try::into_result`.
44+
Self::Loop(ForLoop) | Self::Match(ForLoopDesugar | TryDesugar | AwaitDesugar) => {
45+
return None;
46+
}
4947

50-
Self::Loop(While)
51-
| Self::Loop(WhileLet)
52-
| Self::Match(WhileDesugar | WhileLetDesugar) => {
53-
&[sym::const_loop, sym::const_if_match]
48+
Self::Loop(Loop | While | WhileLet) | Self::Match(WhileDesugar | WhileLetDesugar) => {
49+
&[sym::const_loop]
5450
}
5551

56-
// A `for` loop's desugaring contains a call to `IntoIterator::into_iter`,
57-
// so they are not yet allowed with `#![feature(const_loop)]`.
58-
_ => return None,
52+
// All other matches are allowed.
53+
Self::Match(Normal | IfDesugar { .. } | IfLetDesugar { .. }) => &[],
5954
};
6055

6156
Some(gates)
@@ -114,17 +109,6 @@ impl<'tcx> CheckConstVisitor<'tcx> {
114109
match missing_gates.as_slice() {
115110
&[] => struct_span_err!(self.tcx.sess, span, E0744, "{}", msg).emit(),
116111

117-
// If the user enabled `#![feature(const_loop)]` but not `#![feature(const_if_match)]`,
118-
// explain why their `while` loop is being rejected.
119-
&[gate @ sym::const_if_match] if required_gates.contains(&sym::const_loop) => {
120-
feature_err(&self.tcx.sess.parse_sess, gate, span, &msg)
121-
.note(
122-
"`#![feature(const_loop)]` alone is not sufficient, \
123-
since this loop expression contains an implicit conditional",
124-
)
125-
.emit();
126-
}
127-
128112
&[missing_primary, ref missing_secondary @ ..] => {
129113
let mut err = feature_err(&self.tcx.sess.parse_sess, missing_primary, span, &msg);
130114

@@ -175,15 +159,6 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
175159
self.recurse_into(kind, |this| intravisit::walk_body(this, body));
176160
}
177161

178-
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
179-
if self.const_kind.is_some() {
180-
if let hir::PatKind::Or { .. } = p.kind {
181-
self.const_check_violated(NonConstExpr::OrPattern, p.span);
182-
}
183-
}
184-
intravisit::walk_pat(self, p)
185-
}
186-
187162
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
188163
match &e.kind {
189164
// Skip the following checks if we are not currently in a const context.

0 commit comments

Comments
 (0)