Skip to content

Remove the unecessary ParamBounds struct #26354

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

Merged
merged 1 commit into from
Jun 23, 2015
Merged
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
88 changes: 17 additions & 71 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,6 @@ pub fn parse_substs_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum, pos: us
parse_substs(&mut st, conv)
}

pub fn parse_bounds_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum,
pos: usize, tcx: &ty::ctxt<'tcx>, conv: F)
-> ty::ParamBounds<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_bounds(&mut st, conv)
}

pub fn parse_existential_bounds_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum,
pos: usize, tcx: &ty::ctxt<'tcx>, conv: F)
-> ty::ExistentialBounds<'tcx> where
Expand Down Expand Up @@ -879,11 +870,23 @@ fn parse_existential_bounds_<'a,'tcx, F>(st: &mut PState<'a,'tcx>,
-> ty::ExistentialBounds<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
let ty::ParamBounds { trait_bounds, mut region_bounds, builtin_bounds, projection_bounds } =
parse_bounds_(st, conv);
assert_eq!(region_bounds.len(), 1);
assert_eq!(trait_bounds.len(), 0);
let region_bound = region_bounds.pop().unwrap();
let builtin_bounds = parse_builtin_bounds_(st, conv);
let region_bound = parse_region_(st, conv);
let mut projection_bounds = Vec::new();

loop {
match next(st) {
'P' => {
projection_bounds.push(
ty::Binder(parse_projection_predicate_(st, conv)));
}
'.' => { break; }
c => {
panic!("parse_bounds: bad bounds ('{}')", c)
}
}
}

return ty::ExistentialBounds { region_bound: region_bound,
builtin_bounds: builtin_bounds,
projection_bounds: projection_bounds };
Expand Down Expand Up @@ -923,60 +926,3 @@ fn parse_builtin_bounds_<F>(st: &mut PState, _conv: &mut F) -> ty::BuiltinBounds
}
}
}

fn parse_bounds<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, mut conv: F)
-> ty::ParamBounds<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
parse_bounds_(st, &mut conv)
}

fn parse_bounds_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
-> ty::ParamBounds<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
let builtin_bounds = parse_builtin_bounds_(st, conv);

let region_bounds = parse_region_bounds_(st, conv);

let mut param_bounds = ty::ParamBounds {
region_bounds: region_bounds,
builtin_bounds: builtin_bounds,
trait_bounds: Vec::new(),
projection_bounds: Vec::new(),
};


loop {
match next(st) {
'I' => {
param_bounds.trait_bounds.push(
ty::Binder(parse_trait_ref_(st, conv)));
}
'P' => {
param_bounds.projection_bounds.push(
ty::Binder(parse_projection_predicate_(st, conv)));
}
'.' => {
return param_bounds;
}
c => {
panic!("parse_bounds: bad bounds ('{}')", c)
}
}
}
}

fn parse_region_bounds_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
-> Vec<ty::Region> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
let mut region_bounds = Vec::new();
loop {
match next(st) {
'R' => { region_bounds.push(parse_region_(st, conv)); }
'.' => { return region_bounds; }
c => { panic!("parse_bounds: bad bounds ('{}')", c); }
}
}
}
16 changes: 1 addition & 15 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,23 +380,9 @@ pub fn enc_builtin_bounds(w: &mut Encoder, _cx: &ctxt, bs: &ty::BuiltinBounds) {
pub fn enc_existential_bounds<'a,'tcx>(w: &mut Encoder,
cx: &ctxt<'a,'tcx>,
bs: &ty::ExistentialBounds<'tcx>) {
let param_bounds = ty::ParamBounds { trait_bounds: vec!(),
region_bounds: vec!(bs.region_bound),
builtin_bounds: bs.builtin_bounds,
projection_bounds: bs.projection_bounds.clone() };
enc_bounds(w, cx, &param_bounds);
}

pub fn enc_bounds<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
bs: &ty::ParamBounds<'tcx>) {
enc_builtin_bounds(w, cx, &bs.builtin_bounds);

enc_region_bounds(w, cx, &bs.region_bounds);

for tp in &bs.trait_bounds {
mywrite!(w, "I");
enc_trait_ref(w, cx, tp.0);
}
enc_region(w, cx, bs.region_bound);

for tp in &bs.projection_bounds {
mywrite!(w, "P");
Expand Down
11 changes: 3 additions & 8 deletions src/librustc/middle/implicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use middle::infer::{InferCtxt, GenericKind};
use middle::subst::Substs;
use middle::traits;
use middle::ty::{self, RegionEscape, ToPolyTraitRef, Ty};
use middle::ty::{self, RegionEscape, ToPolyTraitRef, AsPredicate, Ty};
use middle::ty_fold::{TypeFoldable, TypeFolder};

use syntax::ast;
Expand Down Expand Up @@ -444,13 +444,8 @@ pub fn object_region_bounds<'tcx>(
let substs = tcx.mk_substs(principal.0.substs.with_self_ty(open_ty));
let trait_refs = vec!(ty::Binder(ty::TraitRef::new(principal.0.def_id, substs)));

let param_bounds = ty::ParamBounds {
region_bounds: Vec::new(),
builtin_bounds: others,
trait_bounds: trait_refs,
projection_bounds: Vec::new(), // not relevant to computing region bounds
};
let mut predicates = others.to_predicates(tcx, open_ty);
predicates.extend(trait_refs.iter().map(|t| t.as_predicate()));

let predicates = ty::predicates(tcx, open_ty, &param_bounds);
ty::required_region_bounds(tcx, open_ty, predicates)
}
72 changes: 13 additions & 59 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,21 +1847,8 @@ pub enum type_err<'tcx> {
terr_projection_bounds_length(expected_found<usize>),
}

/// Bounds suitable for a named type parameter like `A` in `fn foo<A>`
/// as well as the existential type parameter in an object type.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct ParamBounds<'tcx> {
pub region_bounds: Vec<ty::Region>,
pub builtin_bounds: BuiltinBounds,
pub trait_bounds: Vec<PolyTraitRef<'tcx>>,
pub projection_bounds: Vec<PolyProjectionPredicate<'tcx>>,
}

/// Bounds suitable for an existentially quantified type parameter
/// such as those that appear in object types or closure types. The
/// major difference between this case and `ParamBounds` is that
/// general purpose trait bounds are omitted and there must be
/// *exactly one* region.
/// such as those that appear in object types or closure types.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct ExistentialBounds<'tcx> {
pub region_bound: ty::Region,
Expand All @@ -1873,13 +1860,24 @@ pub struct ExistentialBounds<'tcx> {
pub struct BuiltinBounds(EnumSet<BuiltinBound>);

impl BuiltinBounds {
pub fn empty() -> BuiltinBounds {
pub fn empty() -> BuiltinBounds {
BuiltinBounds(EnumSet::new())
}

pub fn iter(&self) -> enum_set::Iter<BuiltinBound> {
self.into_iter()
}

pub fn to_predicates<'tcx>(&self,
tcx: &ty::ctxt<'tcx>,
self_ty: Ty<'tcx>) -> Vec<Predicate<'tcx>> {
self.iter().filter_map(|builtin_bound|
match traits::trait_ref_for_builtin_bound(tcx, builtin_bound, self_ty) {
Ok(trait_ref) => Some(trait_ref.as_predicate()),
Err(ErrorReported) => { None }
}
).collect()
}
}

impl ops::Deref for BuiltinBounds {
Expand Down Expand Up @@ -3703,17 +3701,6 @@ impl<'tcx> ItemSubsts<'tcx> {
}
}

impl<'tcx> ParamBounds<'tcx> {
pub fn empty() -> ParamBounds<'tcx> {
ParamBounds {
builtin_bounds: BuiltinBounds::empty(),
trait_bounds: Vec::new(),
region_bounds: Vec::new(),
projection_bounds: Vec::new(),
}
}
}

// Type utilities

pub fn type_is_nil(ty: Ty) -> bool {
Expand Down Expand Up @@ -6142,39 +6129,6 @@ pub fn lookup_super_predicates<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId)
|| csearch::get_super_predicates(cx, did))
}

pub fn predicates<'tcx>(
tcx: &ctxt<'tcx>,
param_ty: Ty<'tcx>,
bounds: &ParamBounds<'tcx>)
-> Vec<Predicate<'tcx>>
{
let mut vec = Vec::new();

for builtin_bound in &bounds.builtin_bounds {
match traits::trait_ref_for_builtin_bound(tcx, builtin_bound, param_ty) {
Ok(trait_ref) => { vec.push(trait_ref.as_predicate()); }
Err(ErrorReported) => { }
}
}

for &region_bound in &bounds.region_bounds {
// account for the binder being introduced below; no need to shift `param_ty`
// because, at present at least, it can only refer to early-bound regions
let region_bound = ty_fold::shift_region(region_bound, 1);
vec.push(ty::Binder(ty::OutlivesPredicate(param_ty, region_bound)).as_predicate());
}

for bound_trait_ref in &bounds.trait_bounds {
vec.push(bound_trait_ref.as_predicate());
}

for projection in &bounds.projection_bounds {
vec.push(projection.as_predicate());
}

vec
}

/// Get the attributes of a definition.
pub fn get_attrs<'tcx>(tcx: &'tcx ctxt, did: DefId)
-> Cow<'tcx, [ast::Attribute]> {
Expand Down
11 changes: 0 additions & 11 deletions src/librustc/middle/ty_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,6 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialBounds<'tcx> {
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::ParamBounds<'tcx> {
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ParamBounds<'tcx> {
ty::ParamBounds {
region_bounds: self.region_bounds.fold_with(folder),
builtin_bounds: self.builtin_bounds.fold_with(folder),
trait_bounds: self.trait_bounds.fold_with(folder),
projection_bounds: self.projection_bounds.fold_with(folder),
}
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TypeParameterDef<'tcx> {
ty::TypeParameterDef {
Expand Down
33 changes: 0 additions & 33 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,23 +347,6 @@ impl fmt::Debug for subst::RegionSubsts {
}
}


impl<'tcx> fmt::Debug for ty::ParamBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{:?}", self.builtin_bounds));
let mut bounds = self.trait_bounds.iter();
if self.builtin_bounds.is_empty() {
if let Some(bound) = bounds.next() {
try!(write!(f, "{:?}", bound));
}
}
for bound in bounds {
try!(write!(f, " + {:?}", bound));
}
Ok(())
}
}

impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// when printing out the debug representation, we don't need
Expand Down Expand Up @@ -539,22 +522,6 @@ impl<'tcx> fmt::Debug for ty::MethodObject<'tcx> {
}
}

impl<'tcx> fmt::Display for ty::ParamBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.builtin_bounds));
let mut bounds = self.trait_bounds.iter();
if self.builtin_bounds.is_empty() {
if let Some(bound) = bounds.next() {
try!(write!(f, "{}", bound));
}
}
for bound in bounds {
try!(write!(f, " + {}", bound));
}
Ok(())
}
}

impl<'tcx> fmt::Debug for ty::ExistentialBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut empty = true;
Expand Down
47 changes: 46 additions & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ use middle::resolve_lifetime as rl;
use middle::privacy::{AllPublic, LastMod};
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs};
use middle::traits;
use middle::ty::{self, RegionEscape, Ty};
use middle::ty::{self, RegionEscape, Ty, AsPredicate};
use middle::ty_fold;
use rscope::{self, UnelidableRscope, RegionScope, ElidableRscope, ExplicitRscope,
ObjectLifetimeDefaultRscope, ShiftedRscope, BindingRscope};
use util::common::{ErrorReported, FN_OUTPUT_NAME};
Expand Down Expand Up @@ -2191,3 +2192,47 @@ fn report_lifetime_number_error(tcx: &ty::ctxt, span: Span, number: usize, expec
"wrong number of lifetime parameters: expected {}, found {}",
expected, number);
}

// A helper struct for conveniently grouping a set of bounds which we pass to
// and return from functions in multiple places.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Bounds<'tcx> {
pub region_bounds: Vec<ty::Region>,
pub builtin_bounds: ty::BuiltinBounds,
pub trait_bounds: Vec<ty::PolyTraitRef<'tcx>>,
pub projection_bounds: Vec<ty::PolyProjectionPredicate<'tcx>>,
}

impl<'tcx> Bounds<'tcx> {
pub fn predicates(&self,
tcx: &ty::ctxt<'tcx>,
param_ty: Ty<'tcx>)
-> Vec<ty::Predicate<'tcx>>
{
let mut vec = Vec::new();

for builtin_bound in &self.builtin_bounds {
match traits::trait_ref_for_builtin_bound(tcx, builtin_bound, param_ty) {
Ok(trait_ref) => { vec.push(trait_ref.as_predicate()); }
Err(ErrorReported) => { }
}
}

for &region_bound in &self.region_bounds {
// account for the binder being introduced below; no need to shift `param_ty`
// because, at present at least, it can only refer to early-bound regions
let region_bound = ty_fold::shift_region(region_bound, 1);
vec.push(ty::Binder(ty::OutlivesPredicate(param_ty, region_bound)).as_predicate());
}

for bound_trait_ref in &self.trait_bounds {
vec.push(bound_trait_ref.as_predicate());
}

for projection in &self.projection_bounds {
vec.push(projection.as_predicate());
}

vec
}
}
Loading