Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TypeVisitor: use ControlFlow in rustc_{mir,privacy,traits,typeck}
  • Loading branch information
LeSeulArtichaut committed Oct 30, 2020
1 parent 4fe735b commit 61f8182
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 122 deletions.
23 changes: 10 additions & 13 deletions compiler/rustc_mir/src/interpret/util.rs
@@ -1,6 +1,7 @@
use rustc_middle::mir::interpret::InterpResult;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
use std::convert::TryInto;
use std::ops::ControlFlow;

/// Returns `true` if a used generic parameter requires substitution.
crate fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
Expand All @@ -17,24 +18,24 @@ where
};

impl<'tcx> TypeVisitor<'tcx> for UsedParamsNeedSubstVisitor<'tcx> {
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<(), ()> {
if !c.needs_subst() {
return false;
return ControlFlow::CONTINUE;
}

match c.val {
ty::ConstKind::Param(..) => true,
ty::ConstKind::Param(..) => ControlFlow::BREAK,
_ => c.super_visit_with(self),
}
}

fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
if !ty.needs_subst() {
return false;
return ControlFlow::CONTINUE;
}

match *ty.kind() {
ty::Param(_) => true,
ty::Param(_) => ControlFlow::BREAK,
ty::Closure(def_id, substs)
| ty::Generator(def_id, substs, ..)
| ty::FnDef(def_id, substs) => {
Expand All @@ -50,11 +51,7 @@ where
match (is_used, subst.needs_subst()) {
// Just in case there are closures or generators within this subst,
// recurse.
(true, true) if subst.super_visit_with(self) => {
// Only return when we find a parameter so the remaining substs
// are not skipped.
return true;
}
(true, true) => return subst.super_visit_with(self),
// Confirm that polymorphization replaced the parameter with
// `ty::Param`/`ty::ConstKind::Param`.
(false, true) if cfg!(debug_assertions) => match subst.unpack() {
Expand All @@ -69,15 +66,15 @@ where
_ => {}
}
}
false
ControlFlow::CONTINUE
}
_ => ty.super_visit_with(self),
}
}
}

let mut vis = UsedParamsNeedSubstVisitor { tcx };
if ty.visit_with(&mut vis) {
if ty.visit_with(&mut vis) == ControlFlow::BREAK {
throw_inval!(TooGeneric);
} else {
Ok(())
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/lib.rs
Expand Up @@ -27,6 +27,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(option_expect_none)]
#![feature(or_patterns)]
#![feature(once_cell)]
#![feature(control_flow_enum)]
#![recursion_limit = "256"]

#[macro_use]
Expand Down
45 changes: 28 additions & 17 deletions compiler/rustc_mir/src/monomorphize/polymorphize.rs
Expand Up @@ -20,6 +20,7 @@ use rustc_middle::ty::{
};
use rustc_span::symbol::sym;
use std::convert::TryInto;
use std::ops::ControlFlow;

/// Provide implementations of queries relating to polymorphization analysis.
pub fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -138,7 +139,7 @@ fn mark_used_by_predicates<'tcx>(
// predicate is used.
let any_param_used = {
let mut vis = HasUsedGenericParams { unused_parameters };
predicate.visit_with(&mut vis)
predicate.visit_with(&mut vis) == ControlFlow::BREAK
};

if any_param_used {
Expand Down Expand Up @@ -249,17 +250,17 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
}

impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<(), ()> {
debug!("visit_const: c={:?}", c);
if !c.has_param_types_or_consts() {
return false;
return ControlFlow::CONTINUE;
}

match c.val {
ty::ConstKind::Param(param) => {
debug!("visit_const: param={:?}", param);
self.unused_parameters.clear(param.index);
false
ControlFlow::CONTINUE
}
ty::ConstKind::Unevaluated(def, _, Some(p))
// Avoid considering `T` unused when constants are of the form:
Expand All @@ -270,41 +271,41 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
// the generic parameters, instead, traverse the promoted MIR.
let promoted = self.tcx.promoted_mir(def.did);
self.visit_body(&promoted[p]);
false
ControlFlow::CONTINUE
}
ty::ConstKind::Unevaluated(def, unevaluated_substs, None)
if self.tcx.def_kind(def.did) == DefKind::AnonConst =>
{
self.visit_child_body(def.did, unevaluated_substs);
false
ControlFlow::CONTINUE
}
_ => c.super_visit_with(self),
}
}

fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
debug!("visit_ty: ty={:?}", ty);
if !ty.has_param_types_or_consts() {
return false;
return ControlFlow::CONTINUE;
}

match *ty.kind() {
ty::Closure(def_id, substs) | ty::Generator(def_id, substs, ..) => {
debug!("visit_ty: def_id={:?}", def_id);
// Avoid cycle errors with generators.
if def_id == self.def_id {
return false;
return ControlFlow::CONTINUE;
}

// Consider any generic parameters used by any closures/generators as used in the
// parent.
self.visit_child_body(def_id, substs);
false
ControlFlow::CONTINUE
}
ty::Param(param) => {
debug!("visit_ty: param={:?}", param);
self.unused_parameters.clear(param.index);
false
ControlFlow::CONTINUE
}
_ => ty.super_visit_with(self),
}
Expand All @@ -317,28 +318,38 @@ struct HasUsedGenericParams<'a> {
}

impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a> {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<(), ()> {
debug!("visit_const: c={:?}", c);
if !c.has_param_types_or_consts() {
return false;
return ControlFlow::CONTINUE;
}

match c.val {
ty::ConstKind::Param(param) => {
!self.unused_parameters.contains(param.index).unwrap_or(false)
if self.unused_parameters.contains(param.index).unwrap_or(false) {
ControlFlow::CONTINUE
} else {
ControlFlow::BREAK
}
}
_ => c.super_visit_with(self),
}
}

fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
debug!("visit_ty: ty={:?}", ty);
if !ty.has_param_types_or_consts() {
return false;
return ControlFlow::CONTINUE;
}

match ty.kind() {
ty::Param(param) => !self.unused_parameters.contains(param.index).unwrap_or(false),
ty::Param(param) => {
if self.unused_parameters.contains(param.index).unwrap_or(false) {
ControlFlow::CONTINUE
} else {
ControlFlow::BREAK
}
}
_ => ty.super_visit_with(self),
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir/src/util/pretty.rs
Expand Up @@ -19,6 +19,7 @@ use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
use rustc_target::abi::Size;
use std::ops::ControlFlow;

const INDENT: &str = " ";
/// Alignment for lining up comments following MIR statements
Expand Down Expand Up @@ -639,7 +640,7 @@ pub fn write_allocations<'tcx>(
}
struct CollectAllocIds(BTreeSet<AllocId>);
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<(), ()> {
if let ty::ConstKind::Value(val) = c.val {
self.0.extend(alloc_ids_from_const(val));
}
Expand Down

0 comments on commit 61f8182

Please sign in to comment.