1
Fork 0

Auto merge of #78182 - LeSeulArtichaut:ty-visitor-contolflow, r=lcnr,oli-obk

TypeVisitor: use `std::ops::ControlFlow` instead of `bool`

Implements MCP rust-lang/compiler-team#374.

Blocked on FCP in rust-lang/compiler-team#374.
r? `@lcnr` cc `@jonas-schievink`
This commit is contained in:
bors 2020-10-30 22:53:55 +00:00
commit 0d033dee3e
38 changed files with 523 additions and 376 deletions

View file

@ -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>
@ -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) => {
@ -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() {
@ -69,7 +66,7 @@ where
_ => {}
}
}
false
ControlFlow::CONTINUE
}
_ => ty.super_visit_with(self),
}
@ -77,7 +74,7 @@ where
}
let mut vis = UsedParamsNeedSubstVisitor { tcx };
if ty.visit_with(&mut vis) {
if ty.visit_with(&mut vis).is_break() {
throw_inval!(TooGeneric);
} else {
Ok(())

View file

@ -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]

View file

@ -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) {
@ -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).is_break()
};
if any_param_used {
@ -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:
@ -270,22 +271,22 @@ 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() {
@ -293,18 +294,18 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
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),
}
@ -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),
}
}

View file

@ -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
@ -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));
}