Skip to content
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

Revert two unapproved changes to rustc_typeck. #59789

Merged
merged 5 commits into from Nov 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -95,8 +95,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generics_of => {
tcx.arena.alloc(cdata.get_generics(def_id.index, tcx.sess))
}
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
predicates_defined_on => { cdata.get_predicates_defined_on(def_id.index, tcx) }
explicit_predicates_of => { cdata.get_explicit_predicates(def_id.index, tcx) }
inferred_outlives_of => { cdata.get_inferred_outlives(def_id.index, tcx) }
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
trait_def => {
tcx.arena.alloc(cdata.get_trait_def(def_id.index, tcx.sess))
Expand Down
12 changes: 7 additions & 5 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -658,20 +658,22 @@ impl<'a, 'tcx> CrateMetadata {
tcx.alloc_adt_def(did, adt_kind, variants, repr)
}

crate fn get_predicates(
crate fn get_explicit_predicates(
&self,
item_id: DefIndex,
tcx: TyCtxt<'tcx>,
) -> ty::GenericPredicates<'tcx> {
self.root.per_def.predicates.get(self, item_id).unwrap().decode((self, tcx))
self.root.per_def.explicit_predicates.get(self, item_id).unwrap().decode((self, tcx))
}

crate fn get_predicates_defined_on(
crate fn get_inferred_outlives(
&self,
item_id: DefIndex,
tcx: TyCtxt<'tcx>,
) -> ty::GenericPredicates<'tcx> {
self.root.per_def.predicates_defined_on.get(self, item_id).unwrap().decode((self, tcx))
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
self.root.per_def.inferred_outlives.get(self, item_id).map(|predicates| {
predicates.decode((self, tcx))
}).unwrap_or_default()
}

crate fn get_super_predicates(
Expand Down
64 changes: 32 additions & 32 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -76,8 +76,8 @@ struct PerDefTables<'tcx> {
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
variances: PerDefTable<Lazy<[ty::Variance]>>,
generics: PerDefTable<Lazy<ty::Generics>>,
predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
predicates_defined_on: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
explicit_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
inferred_outlives: PerDefTable<Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,

mir: PerDefTable<Lazy<mir::Body<'tcx>>>,
Expand Down Expand Up @@ -524,8 +524,8 @@ impl<'tcx> EncodeContext<'tcx> {
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
variances: self.per_def.variances.encode(&mut self.opaque),
generics: self.per_def.generics.encode(&mut self.opaque),
predicates: self.per_def.predicates.encode(&mut self.opaque),
predicates_defined_on: self.per_def.predicates_defined_on.encode(&mut self.opaque),
explicit_predicates: self.per_def.explicit_predicates.encode(&mut self.opaque),
inferred_outlives: self.per_def.inferred_outlives.encode(&mut self.opaque),
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),

mir: self.per_def.mir.encode(&mut self.opaque),
Expand Down Expand Up @@ -676,7 +676,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand Down Expand Up @@ -719,7 +720,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand Down Expand Up @@ -777,7 +779,8 @@ impl EncodeContext<'tcx> {
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_struct_ctor(&mut self, adt_def_id: DefId, def_id: DefId) {
Expand Down Expand Up @@ -820,7 +823,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand All @@ -830,15 +834,18 @@ impl EncodeContext<'tcx> {
record!(self.per_def.generics[def_id] <- self.tcx.generics_of(def_id));
}

fn encode_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_predicates({:?})", def_id);
record!(self.per_def.predicates[def_id] <- self.tcx.predicates_of(def_id));
fn encode_explicit_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_explicit_predicates({:?})", def_id);
record!(self.per_def.explicit_predicates[def_id] <-
self.tcx.explicit_predicates_of(def_id));
}

fn encode_predicates_defined_on(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_predicates_defined_on({:?})", def_id);
record!(self.per_def.predicates_defined_on[def_id] <-
self.tcx.predicates_defined_on(def_id))
fn encode_inferred_outlives(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_inferred_outlives({:?})", def_id);
let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
if !inferred_outlives.is_empty() {
record!(self.per_def.inferred_outlives[def_id] <- inferred_outlives);
}
}

fn encode_super_predicates(&mut self, def_id: DefId) {
Expand Down Expand Up @@ -920,7 +927,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand Down Expand Up @@ -987,7 +995,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
let mir = match ast_item.kind {
hir::ImplItemKind::Const(..) => true,
hir::ImplItemKind::Method(ref sig, _) => {
Expand Down Expand Up @@ -1261,22 +1270,11 @@ impl EncodeContext<'tcx> {
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => {
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}
_ => {}
}
// The only time that `predicates_defined_on` is used (on
// an external item) is for traits, during chalk lowering,
// so only encode it in that case as an efficiency
// hack. (No reason not to expand it in the future if
// necessary.)
match item.kind {
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => {
self.encode_predicates_defined_on(def_id);
}
_ => {} // not *wrong* for other kinds of items, but not needed
}
match item.kind {
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => {
Expand Down Expand Up @@ -1378,7 +1376,8 @@ impl EncodeContext<'tcx> {
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
self.encode_item_type(def_id);
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand Down Expand Up @@ -1589,7 +1588,8 @@ impl EncodeContext<'tcx> {
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_predicates(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/librustc_metadata/schema.rs
Expand Up @@ -244,8 +244,13 @@ crate struct LazyPerDefTables<'tcx> {
pub inherent_impls: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
pub variances: Lazy!(PerDefTable<Lazy<[ty::Variance]>>),
pub generics: Lazy!(PerDefTable<Lazy<ty::Generics>>),
pub predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
pub predicates_defined_on: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
pub explicit_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
// doesn't handle shorthands in its own (de)serialization impls,
// as it's an `enum` for which we want to derive (de)serialization,
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
// Also, as an optimization, a missing entry indicates an empty `&[]`.
pub inferred_outlives: Lazy!(PerDefTable<Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
pub super_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),

pub mir: Lazy!(PerDefTable<Lazy!(mir::Body<'tcx>)>),
Expand Down
70 changes: 13 additions & 57 deletions src/librustc_typeck/collect.rs
Expand Up @@ -1142,10 +1142,6 @@ fn report_assoc_ty_on_inherent_impl(tcx: TyCtxt<'_>, span: Span) {
);
}

fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
checked_type_of(tcx, def_id, true).unwrap()
}

fn infer_placeholder_type(
tcx: TyCtxt<'_>,
def_id: DefId,
Expand Down Expand Up @@ -1189,26 +1185,14 @@ fn infer_placeholder_type(
ty
}

/// Same as [`type_of`] but returns [`Option`] instead of failing.
///
/// If you want to fail anyway, you can set the `fail` parameter to true, but in this case,
/// you'd better just call [`type_of`] directly.
pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<'_>> {
fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
use rustc::hir::*;

let hir_id = match tcx.hir().as_local_hir_id(def_id) {
Some(hir_id) => hir_id,
None => {
if !fail {
return None;
}
bug!("invalid node");
}
};
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();

let icx = ItemCtxt::new(tcx, def_id);

Some(match tcx.hir().get(hir_id) {
match tcx.hir().get(hir_id) {
Node::TraitItem(item) => match item.kind {
TraitItemKind::Method(..) => {
let substs = InternalSubsts::identity_for_item(tcx, def_id);
Expand All @@ -1225,9 +1209,6 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
},
TraitItemKind::Type(_, Some(ref ty)) => icx.to_ty(ty),
TraitItemKind::Type(_, None) => {
if !fail {
return None;
}
span_bug!(item.span, "associated type missing default");
}
},
Expand Down Expand Up @@ -1321,9 +1302,6 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
| ItemKind::GlobalAsm(..)
| ItemKind::ExternCrate(..)
| ItemKind::Use(..) => {
if !fail {
return None;
}
span_bug!(
item.span,
"compute_type_of_item: unexpected item type: {:?}",
Expand Down Expand Up @@ -1361,7 +1339,7 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
..
}) => {
if gen.is_some() {
return Some(tcx.typeck_tables_of(def_id).node_type(hir_id));
return tcx.typeck_tables_of(def_id).node_type(hir_id);
}

let substs = InternalSubsts::identity_for_item(tcx, def_id);
Expand Down Expand Up @@ -1436,13 +1414,9 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
.map(|(index, _)| index)
.next()
})
.or_else(|| {
if !fail {
None
} else {
bug!("no arg matching AnonConst in path")
}
})?;
.unwrap_or_else(|| {
bug!("no arg matching AnonConst in path");
});

// We've encountered an `AnonConst` in some path, so we need to
// figure out which generic parameter it corresponds to and return
Expand All @@ -1452,8 +1426,7 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
tcx.generics_of(tcx.parent(def_id).unwrap())
}
Res::Def(_, def_id) => tcx.generics_of(def_id),
Res::Err => return Some(tcx.types.err),
_ if !fail => return None,
Res::Err => return tcx.types.err,
res => {
tcx.sess.delay_span_bug(
DUMMY_SP,
Expand All @@ -1462,7 +1435,7 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
res,
),
);
return Some(tcx.types.err);
return tcx.types.err;
}
};

Expand All @@ -1480,24 +1453,18 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
// probably from an extra arg where one is not needed.
.unwrap_or(tcx.types.err)
} else {
if !fail {
return None;
}
tcx.sess.delay_span_bug(
DUMMY_SP,
&format!(
"unexpected const parent path {:?}",
parent_node,
),
);
return Some(tcx.types.err);
return tcx.types.err;
}
}

x => {
if !fail {
return None;
}
tcx.sess.delay_span_bug(
DUMMY_SP,
&format!(
Expand Down Expand Up @@ -1547,21 +1514,13 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
}
ty
}
x => {
if !fail {
return None;
}
bug!("unexpected non-type Node::GenericParam: {:?}", x)
},
x => bug!("unexpected non-type Node::GenericParam: {:?}", x),
},

x => {
if !fail {
return None;
}
bug!("unexpected sort of node in type_of_def_id(): {:?}", x);
}
})
}
}

fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
Expand Down Expand Up @@ -2071,10 +2030,7 @@ fn explicit_predicates_of(
}
}

let hir_id = match tcx.hir().as_local_hir_id(def_id) {
Some(hir_id) => hir_id,
None => return tcx.predicates_of(def_id),
};
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
let node = tcx.hir().get(hir_id);

let mut is_trait = None;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_typeck/lib.rs
Expand Up @@ -109,8 +109,6 @@ use util::common::time;
use std::iter;

use astconv::{AstConv, Bounds};
pub use collect::checked_type_of;

pub struct TypeAndSubsts<'tcx> {
substs: SubstsRef<'tcx>,
ty: Ty<'tcx>,
Expand Down