Skip to content

Remove single-variant Guard enums. #58283

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 1 addition & 3 deletions src/librustc/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
// expression to target
let guard_start = self.add_dummy_node(&[pat_exit]);
// Visit the guard expression
let guard_exit = match guard {
hir::Guard::If(ref e) => self.expr(e, guard_start),
};
let guard_exit = self.expr(guard, guard_start);
// #47295: We used to have very special case code
// here for when a pair of arms are both formed
// solely from constants, and if so, not add these
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,10 +1092,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {

pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
walk_list!(visitor, visit_pat, &arm.pats);
if let Some(ref g) = arm.guard {
match g {
Guard::If(ref e) => visitor.visit_expr(e),
}
if let Some(ref e) = arm.guard {
visitor.visit_expr(e);
}
visitor.visit_expr(&arm.body);
walk_list!(visitor, visit_attribute, &arm.attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ impl<'a> LoweringContext<'a> {
attrs: self.lower_attrs(&arm.attrs),
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
guard: match arm.guard {
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
Some(ref e) => Some(P(self.lower_expr(e))),
_ => None,
},
body: P(self.lower_expr(&arm.body)),
Expand Down
7 changes: 1 addition & 6 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,15 +1198,10 @@ pub struct Local {
pub struct Arm {
pub attrs: HirVec<Attribute>,
pub pats: HirVec<P<Pat>>,
pub guard: Option<Guard>,
pub guard: Option<P<Expr>>,
pub body: P<Expr>,
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum Guard {
If(P<Expr>),
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Field {
pub id: NodeId,
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,14 +1940,10 @@ impl<'a> State<'a> {
self.print_pat(&p)?;
}
self.s.space()?;
if let Some(ref g) = arm.guard {
match g {
hir::Guard::If(e) => {
self.word_space("if")?;
self.print_expr(&e)?;
self.s.space()?;
}
}
if let Some(ref e) = arm.guard {
self.word_space("if")?;
self.print_expr(&e)?;
self.s.space()?;
}
self.word_space("=>")?;

Expand Down
4 changes: 0 additions & 4 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,6 @@ impl_stable_hash_for!(struct hir::Arm {
body
});

impl_stable_hash_for!(enum hir::Guard {
If(expr),
});

impl_stable_hash_for!(struct hir::Field {
id -> _,
hir_id -> _,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
self.walk_pat(discr_cmt.clone(), &pat, mode);
}

if let Some(hir::Guard::If(ref e)) = arm.guard {
if let Some(ref e) = arm.guard {
self.consume_expr(e)
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
let body_succ = self.propagate_through_expr(&arm.body, succ);

let guard_succ = self.propagate_through_opt_expr(
arm.guard.as_ref().map(|hir::Guard::If(e)| &**e),
arm.guard.as_ref().map(|e| &**e),
body_succ
);
// only consider the first pattern; any later patterns must have
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
fn resolve_arm<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, arm: &'tcx hir::Arm) {
visitor.terminating_scopes.insert(arm.body.hir_id.local_id);

if let Some(hir::Guard::If(ref expr)) = arm.guard {
if let Some(ref expr) = arm.guard {
visitor.terminating_scopes.insert(expr.hir_id.local_id);
}

Expand Down
6 changes: 2 additions & 4 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ pub struct Candidate<'pat, 'tcx: 'pat> {
ascriptions: Vec<Ascription<'tcx>>,

// ...and the guard must be evaluated...
guard: Option<Guard<'tcx>>,
guard: Option<ExprRef<'tcx>>,

// ...and then we branch to arm with this index.
arm_index: usize,
Expand Down Expand Up @@ -1242,9 +1242,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

// the block to branch to if the guard fails; if there is no
// guard, this block is simply unreachable
let guard = match guard {
Guard::If(e) => self.hir.mirror(e),
};
let guard = self.hir.mirror(guard);
let source_info = self.source_info(guard.span);
let cond = unpack!(block = self.as_local_operand(block, guard));
if autoref {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,9 @@ fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, arm: &'tcx hir::Arm)
Arm {
patterns: arm.pats.iter().map(|p| cx.pattern_from_hir(p)).collect(),
guard: match arm.guard {
Some(hir::Guard::If(ref e)) => Some(Guard::If(e.to_ref())),
_ => None,
},
Some(ref e) => Some(e.to_ref()),
_ => None,
},
body: arm.body.to_ref(),
// BUG: fix this
lint_level: LintLevel::Inherited,
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_mir/hair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,11 @@ pub struct FruInfo<'tcx> {
#[derive(Clone, Debug)]
pub struct Arm<'tcx> {
pub patterns: Vec<Pattern<'tcx>>,
pub guard: Option<Guard<'tcx>>,
pub guard: Option<ExprRef<'tcx>>,
pub body: ExprRef<'tcx>,
pub lint_level: LintLevel,
}

#[derive(Clone, Debug)]
pub enum Guard<'tcx> {
If(ExprRef<'tcx>),
}

#[derive(Copy, Clone, Debug)]
pub enum LogicalOp {
And,
Expand Down
21 changes: 8 additions & 13 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
}
(pattern, &**pat)
}).collect(),
arm.guard.as_ref().map(|g| match g {
hir::Guard::If(ref e) => &**e,
})
arm.guard.as_ref().map(|e| &**e),
)).collect();

// Bail out early if inlining failed.
Expand Down Expand Up @@ -541,19 +539,16 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
/// assign.
///
/// FIXME: this should be done by borrowck.
fn check_for_mutation_in_guard(cx: &MatchVisitor, guard: &hir::Guard) {
fn check_for_mutation_in_guard(cx: &MatchVisitor, guard: &hir::Expr) {
let mut checker = MutationChecker {
cx,
};
match guard {
hir::Guard::If(expr) =>
ExprUseVisitor::new(&mut checker,
cx.tcx,
cx.param_env,
cx.region_scope_tree,
cx.tables,
None).walk_expr(expr),
};
ExprUseVisitor::new(&mut checker,
cx.tcx,
cx.param_env,
cx.region_scope_tree,
cx.tables,
None).walk_expr(guard)
}

struct MutationChecker<'a, 'tcx: 'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/rvalue_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ fn check_expr_kind<'a, 'tcx>(
let _ = v.check_expr(expr);
for index in hirvec_arm.iter() {
let _ = v.check_expr(&*index.body);
if let Some(hir::Guard::If(ref expr)) = index.guard {
if let Some(ref expr) = index.guard {
let _ = v.check_expr(&expr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2895,7 +2895,7 @@ impl<'a> Resolver<'a> {
// This has to happen *after* we determine which pat_idents are variants.
self.check_consistent_bindings(&arm.pats);

if let Some(ast::Guard::If(ref expr)) = arm.guard {
if let Some(ref expr) = arm.guard {
self.visit_expr(expr)
}
self.visit_expr(&arm.body);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
fn visit_arm(&mut self, arm: &'l ast::Arm) {
self.process_var_decl_multi(&arm.pats);
match arm.guard {
Some(ast::Guard::If(ref expr)) => self.visit_expr(expr),
Some(ref expr) => self.visit_expr(expr),
_ => {}
}
self.visit_expr(&arm.body);
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,9 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
};

for (i, (arm, pats_diverge)) in arms.iter().zip(all_arm_pats_diverge).enumerate() {
if let Some(ref g) = arm.guard {
if let Some(ref e) = arm.guard {
self.diverges.set(pats_diverge);
match g {
hir::Guard::If(e) => self.check_expr_has_type_or_error(e, tcx.types.bool),
};
self.check_expr_has_type_or_error(e, tcx.types.bool);
}

self.diverges.set(pats_diverge);
Expand Down
7 changes: 1 addition & 6 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,15 +859,10 @@ pub struct Local {
pub struct Arm {
pub attrs: Vec<Attribute>,
pub pats: Vec<P<Pat>>,
pub guard: Option<Guard>,
pub guard: Option<P<Expr>>,
pub body: P<Expr>,
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum Guard {
If(P<Expr>),
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Field {
pub ident: Ident,
Expand Down
12 changes: 1 addition & 11 deletions src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ pub trait MutVisitor: Sized {
noop_visit_arm(a, self);
}

fn visit_guard(&mut self, g: &mut Guard) {
noop_visit_guard(g, self);
}

fn visit_pat(&mut self, p: &mut P<Pat>) {
noop_visit_pat(p, self);
}
Expand Down Expand Up @@ -379,16 +375,10 @@ pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
pub fn noop_visit_arm<T: MutVisitor>(Arm { attrs, pats, guard, body }: &mut Arm, vis: &mut T) {
visit_attrs(attrs, vis);
visit_vec(pats, |pat| vis.visit_pat(pat));
visit_opt(guard, |guard| vis.visit_guard(guard));
visit_opt(guard, |guard| vis.visit_expr(guard));
vis.visit_expr(body);
}

pub fn noop_visit_guard<T: MutVisitor>(g: &mut Guard, vis: &mut T) {
match g {
Guard::If(e) => vis.visit_expr(e),
}
}

pub fn noop_visit_ty_binding<T: MutVisitor>(TypeBinding { id, ident, ty, span }: &mut TypeBinding,
vis: &mut T) {
vis.visit_id(id);
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ast::{AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy};
use crate::ast::{GenericBound, TraitBoundModifier};
use crate::ast::Unsafety;
use crate::ast::{Mod, AnonConst, Arg, Arm, Guard, Attribute, BindingMode, TraitItemKind};
use crate::ast::{Mod, AnonConst, Arg, Arm, Attribute, BindingMode, TraitItemKind};
use crate::ast::Block;
use crate::ast::{BlockCheckMode, CaptureBy, Movability};
use crate::ast::{Constness, Crate};
Expand Down Expand Up @@ -3922,7 +3922,7 @@ impl<'a> Parser<'a> {
let attrs = self.parse_outer_attributes()?;
let pats = self.parse_pats()?;
let guard = if self.eat_keyword(keywords::If) {
Some(Guard::If(self.parse_expr()?))
Some(self.parse_expr()?)
} else {
None
};
Expand Down
12 changes: 4 additions & 8 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2729,14 +2729,10 @@ impl<'a> State<'a> {
self.print_outer_attributes(&arm.attrs)?;
self.print_pats(&arm.pats)?;
self.s.space()?;
if let Some(ref g) = arm.guard {
match g {
ast::Guard::If(ref e) => {
self.word_space("if")?;
self.print_expr(e)?;
self.s.space()?;
}
}
if let Some(ref e) = arm.guard {
self.word_space("if")?;
self.print_expr(e)?;
self.s.space()?;
}
self.word_space("=>")?;

Expand Down
6 changes: 2 additions & 4 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,8 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {

pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
walk_list!(visitor, visit_pat, &arm.pats);
if let Some(ref g) = &arm.guard {
match g {
Guard::If(ref e) => visitor.visit_expr(e),
}
if let Some(ref e) = &arm.guard {
visitor.visit_expr(e);
}
visitor.visit_expr(&arm.body);
walk_list!(visitor, visit_attribute, &arm.attrs);
Expand Down