Skip to content

Misc lowering cleanups #51654

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 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4093d37
Use `PathParameters` even if just the `lifetimes` field is used
oli-obk Jun 18, 2018
5181218
Cleanup in preparation of generic extraction
oli-obk Jun 18, 2018
dea4d5b
Generate the `NodeId` for `existential type` in the AST
oli-obk Jun 18, 2018
2b7f86a
Reduce repetition around `lower_method_sig`
oli-obk Jun 19, 2018
0dcb45e
Deduplicate code in `lower_lifetime`
oli-obk Jun 19, 2018
98d4500
Remove some global state from the lowering pass
oli-obk Jun 19, 2018
3ae2e31
Pass generics through to existential impl Trait lowering
oli-obk Jun 19, 2018
3dbe348
Don't generate a new NodeId for universal impl Trait
oli-obk Jun 20, 2018
90a54c1
Generate `DefId`s for impl Trait in the def_collector
oli-obk Jun 20, 2018
2fdb64c
Update ui tests
oli-obk Jun 20, 2018
19f5e5e
While we don't have full existential types yet, there is some duplica…
oli-obk Jun 20, 2018
a83733f
Flatten some occurrences of `[P<T>]` to `[T]`
oli-obk Jun 20, 2018
47732a4
Prevent lowering item ids from behaving differently than lowering items
oli-obk Jun 21, 2018
057cf0f
Update rustdoc
oli-obk Jun 21, 2018
1bf351d
We now have proper locations for the `existential type` items.
oli-obk Jun 22, 2018
356d683
Also place method impl trait into the surrounding module
oli-obk Jun 22, 2018
398a5f8
Ignore existential type items during collection for now
oli-obk Jun 22, 2018
9859a52
Don't try to do impl Trait lowering for `impl Trait for Type` items
oli-obk Jun 22, 2018
a152770
Generics Unification rebase fallout
oli-obk Jun 25, 2018
922249c
Update tests
oli-obk Jun 25, 2018
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
3 changes: 1 addition & 2 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,8 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
}
visitor.visit_lifetime(lifetime);
}
TyImplTraitExistential(item_id, def_id, ref lifetimes) => {
TyImplTraitExistential(_, def_id, ref lifetimes) => {
visitor.visit_def_mention(Def::Existential(def_id));
visitor.visit_nested_item(item_id);
walk_list!(visitor, visit_lifetime, lifetimes);
}
TyTypeof(ref expression) => {
Expand Down
413 changes: 243 additions & 170 deletions src/librustc/hir/lowering.rs

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,27 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let hir_id_owner = self.definitions.node_to_hir_id(id).owner;
let hir_id = self.definitions.node_to_hir_id(id);

if hir_id_owner != self.current_dep_node_owner {
if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
}
None => format!("{:?}", node)
};

if hir_id == ::hir::DUMMY_HIR_ID {
println!("Maybe you forgot to lower the node id {:?}?", id);
}

bug!("inconsistent DepNode for `{}`: \
current_dep_node_owner={}, hir_id.owner={}",
node_str,
self.definitions
.def_path(self.current_dep_node_owner)
.to_string_no_crate(),
self.definitions.def_path(hir_id_owner).to_string_no_crate())
self.definitions.def_path(hir_id.owner).to_string_no_crate())
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_ty(&mut self, ty: &'a Ty) {
match ty.node {
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
TyKind::ImplTrait(node_id, _) => {
self.create_def(node_id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
}
_ => {}
}
visit::walk_ty(self, ty);
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,8 @@ pub enum DefPathData {
StructCtor,
/// A constant expression (see {ast,hir}::AnonConst).
AnonConst,
/// An `impl Trait` type node in argument position.
UniversalImplTrait,
/// An `impl Trait` type node in return position.
ExistentialImplTrait,
/// An `impl Trait` type node
ImplTrait,

/// GlobalMetaData identifies a piece of crate metadata that is global to
/// a whole crate (as opposed to just one item). GlobalMetaData components
Expand Down Expand Up @@ -636,8 +634,7 @@ impl DefPathData {
ClosureExpr |
StructCtor |
AnonConst |
ExistentialImplTrait |
UniversalImplTrait => None
ImplTrait => None
}
}

Expand Down Expand Up @@ -667,8 +664,7 @@ impl DefPathData {
ClosureExpr => "{{closure}}",
StructCtor => "{{constructor}}",
AnonConst => "{{constant}}",
ExistentialImplTrait => "{{exist-impl-Trait}}",
UniversalImplTrait => "{{univ-impl-Trait}}",
ImplTrait => "{{impl-Trait}}",
};

Symbol::intern(s).as_interned_str()
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl PathSegment {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum GenericArg {
Lifetime(Lifetime),
Type(P<Ty>),
Type(Ty),
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
Expand Down Expand Up @@ -412,7 +412,7 @@ impl GenericArgs {
self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
}

pub fn inputs(&self) -> &[P<Ty>] {
pub fn inputs(&self) -> &[Ty] {
if self.parenthesized {
for arg in &self.args {
match arg {
Expand Down Expand Up @@ -1660,7 +1660,7 @@ pub enum Ty_ {
/// The never type (`!`)
TyNever,
/// A tuple (`(A, B, C, D,...)`)
TyTup(HirVec<P<Ty>>),
TyTup(HirVec<Ty>),
/// A path to a type definition (`module::module::...::Type`), or an
/// associated type, e.g. `<Vec<T> as Trait>::Type` or `<T>::Target`.
///
Expand Down Expand Up @@ -1721,7 +1721,7 @@ pub struct Arg {
/// Represents the header (not the body) of a function declaration
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct FnDecl {
pub inputs: HirVec<P<Ty>>,
pub inputs: HirVec<Ty>,
pub output: FunctionRetTy,
pub variadic: bool,
/// True if this function has an `self`, `&self` or `&mut self` receiver
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
};
self.with(scope, |_, this| intravisit::walk_item(this, item));
}
hir::ItemExistential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => {
hir::ItemExistential(hir::ExistTy { .. }) => {
// currently existential type declarations are just generated from impl Trait
// items. doing anything on this node is irrelevant, as we currently don't need
// it.
}
hir::ItemTy(_, ref generics)
| hir::ItemExistential(hir::ExistTy { impl_trait_fn: None, ref generics, .. })
| hir::ItemEnum(_, ref generics)
| hir::ItemStruct(_, ref generics)
| hir::ItemUnion(_, ref generics)
Expand Down Expand Up @@ -1789,7 +1788,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {

fn visit_fn_like_elision(
&mut self,
inputs: &'tcx [P<hir::Ty>],
inputs: &'tcx [hir::Ty],
output: Option<&'tcx P<hir::Ty>>,
) {
debug!("visit_fn_like_elision: enter");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
..
}) => {
(self.tcx.sess.codemap().def_span(span), decl.inputs.iter()
.map(|arg| match arg.clone().into_inner().node {
.map(|arg| match arg.clone().node {
hir::TyTup(ref tys) => ArgKind::Tuple(
Some(arg.span),
tys.iter()
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
data @ DefPathData::AnonConst |
data @ DefPathData::MacroDef(..) |
data @ DefPathData::ClosureExpr |
data @ DefPathData::ExistentialImplTrait |
data @ DefPathData::UniversalImplTrait |
data @ DefPathData::ImplTrait |
data @ DefPathData::GlobalMetaData(..) => {
let parent_def_id = self.parent_def_id(def_id).unwrap();
self.push_item_path(buffer, parent_def_id);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ impl PrintContext {
DefPathData::Field(_) |
DefPathData::StructCtor |
DefPathData::AnonConst |
DefPathData::ExistentialImplTrait |
DefPathData::UniversalImplTrait |
DefPathData::ImplTrait |
DefPathData::GlobalMetaData(_) => {
// if we're making a symbol for something, there ought
// to be a value or type-def or something in there
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ impl<'a> ReplaceBodyWithLoop<'a> {
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output {
fn involves_impl_trait(ty: &ast::Ty) -> bool {
match ty.node {
ast::TyKind::ImplTrait(_) => true,
ast::TyKind::ImplTrait(..) => true,
ast::TyKind::Slice(ref subty) |
ast::TyKind::Array(ref subty, _) |
ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. }) |
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
self.no_questions_in_bounds(bounds, "trait object types", false);
}
TyKind::ImplTrait(ref bounds) => {
TyKind::ImplTrait(_, ref bounds) => {
if !bounds.iter()
.any(|b| if let GenericBound::Trait(..) = *b { true } else { false }) {
self.err_handler().span_err(ty.span, "at least one trait must be specified");
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a> NestedImplTraitVisitor<'a> {

impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
fn visit_ty(&mut self, t: &'a Ty) {
if let TyKind::ImplTrait(_) = t.node {
if let TyKind::ImplTrait(..) = t.node {
if let Some(outer_impl_trait) = self.outer_impl_trait {
struct_span_err!(self.session, t.span, E0666,
"nested `impl Trait` is not allowed")
Expand Down Expand Up @@ -561,7 +561,7 @@ impl<'a> ImplTraitProjectionVisitor<'a> {
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
fn visit_ty(&mut self, t: &'a Ty) {
match t.node {
TyKind::ImplTrait(_) => {
TyKind::ImplTrait(..) => {
if self.is_banned {
struct_span_err!(self.session, t.span, E0667,
"`impl Trait` is not allowed in path parameters")
Expand Down
44 changes: 15 additions & 29 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,12 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
hir::ItemUse(..) => {}
// The interface is empty
hir::ItemGlobalAsm(..) => {}
// Checked by visit_ty
hir::ItemExistential(..) => {}
hir::ItemExistential(..) => {
if item_level.is_some() {
// Reach the (potentially private) type and the API being exposed
self.reach(item.id).ty().predicates();
}
}
// Visit everything
hir::ItemConst(..) | hir::ItemStatic(..) |
hir::ItemFn(..) | hir::ItemTy(..) => {
Expand Down Expand Up @@ -390,17 +394,6 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
module_id = self.tcx.hir.get_parent_node(module_id);
}
}

fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
if let hir::TyImplTraitExistential(item_id, _, _) = ty.node {
if self.get(item_id.id).is_some() {
// Reach the (potentially private) type and the API being exposed
self.reach(item_id.id).ty().predicates();
}
}

intravisit::walk_ty(self, ty);
}
}

impl<'b, 'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
Expand Down Expand Up @@ -1556,8 +1549,15 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
hir::ItemUse(..) => {}
// No subitems
hir::ItemGlobalAsm(..) => {}
// Checked in visit_ty
hir::ItemExistential(..) => {}
hir::ItemExistential(..) => {
// Check the traits being exposed, as they're separate,
// e.g. `impl Iterator<Item=T>` has two predicates,
// `X: Iterator` and `<X as Iterator>::Item == T`,
// where `X` is the `impl Iterator<Item=T>` itself,
// stored in `predicates_of`, not in the `Ty` itself.

self.check(item.id, self.inner_visibility).predicates();
}
// Subitems of these items have inherited publicity
hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
hir::ItemTy(..) => {
Expand Down Expand Up @@ -1655,20 +1655,6 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
// handled in `visit_item` above
}

fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
if let hir::TyImplTraitExistential(ref exist_item, _, _) = ty.node {
// Check the traits being exposed, as they're separate,
// e.g. `impl Iterator<Item=T>` has two predicates,
// `X: Iterator` and `<X as Iterator>::Item == T`,
// where `X` is the `impl Iterator<Item=T>` itself,
// stored in `predicates_of`, not in the `Ty` itself.

self.check(exist_item.id, self.inner_visibility).predicates();
}

intravisit::walk_ty(self, ty);
}

// Don't recurse into expressions in array sizes or const initializers
fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
// Don't recurse into patterns in function arguments
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl Sig for ast::Ty {
let nested = pprust::bounds_to_string(bounds);
Ok(text_sig(nested))
}
ast::TyKind::ImplTrait(ref bounds) => {
ast::TyKind::ImplTrait(_, ref bounds) => {
// FIXME recurse into bounds
let nested = pprust::bounds_to_string(bounds);
Ok(text_sig(format!("impl {}", nested)))
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId) {
convert_variant_ctor(tcx, struct_def.id());
}
},
hir::ItemExistential(..) |
hir::ItemExistential(..) => {}
hir::ItemTy(..) | hir::ItemStatic(..) | hir::ItemConst(..) | hir::ItemFn(..) => {
tcx.generics_of(def_id);
tcx.type_of(def_id);
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ pub struct Arguments {
pub values: Vec<Argument>,
}

impl<'a> Clean<Arguments> for (&'a [P<hir::Ty>], &'a [Spanned<ast::Name>]) {
impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [Spanned<ast::Name>]) {
fn clean(&self, cx: &DocContext) -> Arguments {
Arguments {
values: self.0.iter().enumerate().map(|(i, ty)| {
Expand All @@ -2177,7 +2177,7 @@ impl<'a> Clean<Arguments> for (&'a [P<hir::Ty>], &'a [Spanned<ast::Name>]) {
}
}

impl<'a> Clean<Arguments> for (&'a [P<hir::Ty>], hir::BodyId) {
impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
fn clean(&self, cx: &DocContext) -> Arguments {
let body = cx.tcx.hir.body(self.1);

Expand All @@ -2193,7 +2193,7 @@ impl<'a> Clean<Arguments> for (&'a [P<hir::Ty>], hir::BodyId) {
}

impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
where (&'a [P<hir::Ty>], A): Clean<Arguments>
where (&'a [hir::Ty], A): Clean<Arguments>
{
fn clean(&self, cx: &DocContext) -> FnDecl {
FnDecl {
Expand Down Expand Up @@ -2929,7 +2929,7 @@ impl Clean<Type> for hir::Ty {
}
});
if let Some(ty) = type_.cloned() {
ty_substs.insert(ty_param_def, ty.into_inner().clean(cx));
ty_substs.insert(ty_param_def, ty.clean(cx));
} else if let Some(default) = default.clone() {
ty_substs.insert(ty_param_def,
default.into_inner().clean(cx));
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,11 @@ pub enum TyKind {
TraitObject(GenericBounds, TraitObjectSyntax),
/// An `impl Bound1 + Bound2 + Bound3` type
/// where `Bound` is a trait or a lifetime.
ImplTrait(GenericBounds),
///
/// The `NodeId` exists to prevent lowering from having to
/// generate `NodeId`s on the fly, which would complicate
/// the generation of `existential type` items significantly
ImplTrait(NodeId, GenericBounds),
/// No-op; kept solely so that we can pretty-print faithfully
Paren(P<Ty>),
/// Unused for now
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
TyKind::TraitObject(bounds, syntax) => {
TyKind::TraitObject(bounds.move_map(|b| fld.fold_param_bound(b)), syntax)
}
TyKind::ImplTrait(bounds) => {
TyKind::ImplTrait(bounds.move_map(|b| fld.fold_param_bound(b)))
TyKind::ImplTrait(id, bounds) => {
TyKind::ImplTrait(fld.new_id(id), bounds.move_map(|b| fld.fold_param_bound(b)))
}
TyKind::Mac(mac) => {
TyKind::Mac(fld.fold_mac(mac))
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ impl<'a> Parser<'a> {
// Always parse bounds greedily for better error recovery.
let bounds = self.parse_generic_bounds()?;
impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
TyKind::ImplTrait(bounds)
TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)
} else if self.check_keyword(keywords::Dyn) &&
self.look_ahead(1, |t| t.can_begin_bound() &&
!can_continue_type_after_non_fn_ident(t)) {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ impl<'a> State<'a> {
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
self.print_type_bounds(prefix, &bounds[..])?;
}
ast::TyKind::ImplTrait(ref bounds) => {
ast::TyKind::ImplTrait(_, ref bounds) => {
self.print_type_bounds("impl", &bounds[..])?;
}
ast::TyKind::Array(ref ty, ref length) => {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
visitor.visit_anon_const(length)
}
TyKind::TraitObject(ref bounds, ..) |
TyKind::ImplTrait(ref bounds) => {
TyKind::ImplTrait(_, ref bounds) => {
walk_list!(visitor, visit_param_bound, bounds);
}
TyKind::Typeof(ref expression) => {
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/private-type-in-interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type A = <m::Alias as m::Trait>::X; //~ ERROR type `m::Priv` is private
trait Tr2<T> {}
impl<T> Tr2<T> for u8 {}
fn g() -> impl Tr2<m::Alias> { 0 } //~ ERROR type `m::Priv` is private
//~^ ERROR type `m::Priv` is private
fn g_ext() -> impl Tr2<ext::Alias> { 0 } //~ ERROR type `ext::Priv` is private
//~^ ERROR type `ext::Priv` is private

fn main() {}
Loading