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

WIP: Save path segments #53983

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 13 additions & 4 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ pub struct LoweringContext<'a> {
}

pub trait Resolver {
/// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
/// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
fn resolve_hir_path(
&mut self,
path: &ast::Path,
args: Option<P<hir::GenericArgs>>,
is_value: bool,
) -> hir::Path;

/// Obtain the resolution for a node id
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
Expand All @@ -162,7 +167,7 @@ pub trait Resolver {
span: Span,
crate_root: Option<&str>,
components: &[&str],
params: Option<P<hir::GenericArgs>>,
args: Option<P<hir::GenericArgs>>,
is_value: bool,
) -> hir::Path;
}
Expand Down Expand Up @@ -1309,13 +1314,15 @@ impl<'a> LoweringContext<'a> {
// does not actually exist in the AST.
lctx.items.insert(exist_ty_id.node_id, exist_ty_item);

let def = Def::Existential(DefId::local(exist_ty_def_index));
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`
let path = P(hir::Path {
span: exist_ty_span,
def: Def::Existential(DefId::local(exist_ty_def_index)),
def,
segments: hir_vec![hir::PathSegment {
infer_types: false,
ident: Ident::new(keywords::Invalid.name(), exist_ty_span),
def: None,
args: Some(P(hir::GenericArgs {
parenthesized: false,
bindings: HirVec::new(),
Expand Down Expand Up @@ -1792,8 +1799,10 @@ impl<'a> LoweringContext<'a> {
}
}

let def = self.expect_full_def(segment.id);
hir::PathSegment::new(
segment.ident,
Some(def),
generic_args,
infer_types,
)
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ impl fmt::Display for Path {
pub struct PathSegment {
/// The identifier portion of this path segment.
pub ident: Ident,
pub def: Option<Def>,

/// Type/lifetime parameters attached to this path. They come in
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
Expand All @@ -357,14 +358,16 @@ impl PathSegment {
pub fn from_ident(ident: Ident) -> PathSegment {
PathSegment {
ident,
def: None,
infer_types: true,
args: None,
}
}

pub fn new(ident: Ident, args: GenericArgs, infer_types: bool) -> Self {
pub fn new(ident: Ident, def: Option<Def>, args: GenericArgs, infer_types: bool) -> Self {
PathSegment {
ident,
def,
infer_types,
args: if args.is_empty() {
None
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl_stable_hash_for!(struct hir::Path {

impl_stable_hash_for!(struct hir::PathSegment {
ident -> (ident.name),
def,
infer_types,
args
});
Expand Down
60 changes: 32 additions & 28 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

let mut module_path: Vec<_> = prefix.segments.iter()
.chain(path.segments.iter())
.map(|seg| seg.ident)
.map(|seg| (seg.ident, Some(seg.id)))
.collect();

debug!("build_reduced_graph_for_use_tree: module_path={:?}", module_path);
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
// ergonomically unacceptable.
let emit_uniform_paths_canary =
!uniform_paths_canary_emitted &&
module_path.get(0).map_or(false, |ident| {
module_path.get(0).map_or(false, |(ident, _)| {
!ident.is_path_segment_keyword()
});
if emit_uniform_paths_canary {
Expand All @@ -182,13 +182,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

let source = module_path[0];
// Helper closure to emit a canary with the given base path.
let emit = |this: &mut Self, base: Option<Ident>| {
let emit = |this: &mut Self, base: Option<(Ident, Option<NodeId>)>| {
let subclass = SingleImport {
target: Ident {
name: keywords::Underscore.name().gensymed(),
span: source.span,
span: source.0.span,
},
source,
source: source.0,
result: PerNS {
type_ns: Cell::new(Err(Undetermined)),
value_ns: Cell::new(Err(Undetermined)),
Expand All @@ -199,7 +199,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
this.add_import_directive(
base.into_iter().collect(),
subclass.clone(),
source.span,
source.0.span,
id,
root_use_tree.span,
root_id,
Expand All @@ -210,15 +210,15 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
};

// A single simple `self::x` canary.
emit(self, Some(Ident {
emit(self, Some((Ident {
name: keywords::SelfValue.name(),
span: source.span,
}));
span: source.0.span,
}, source.1)));

// One special unprefixed canary per block scope around
// the import, to detect items unreachable by `self::x`.
let orig_current_module = self.current_module;
let mut span = source.span.modern();
let mut span = source.0.span.modern();
loop {
match self.current_module.kind {
ModuleKind::Block(..) => emit(self, None),
Expand All @@ -244,10 +244,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

if nested {
// Correctly handle `self`
if source.name == keywords::SelfValue.name() {
if source.0.name == keywords::SelfValue.name() {
type_ns_only = true;

let empty_prefix = module_path.last().map_or(true, |ident| {
let empty_prefix = module_path.last().map_or(true, |(ident, _)| {
ident.name == keywords::CrateRoot.name()
});
if empty_prefix {
Expand All @@ -263,20 +263,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
// Replace `use foo::self;` with `use foo;`
source = module_path.pop().unwrap();
if rename.is_none() {
ident = source;
ident = source.0;
}
}
} else {
// Disallow `self`
if source.name == keywords::SelfValue.name() {
if source.0.name == keywords::SelfValue.name() {
resolve_error(self,
use_tree.span,
ResolutionError::SelfImportsOnlyAllowedWithin);
}

// Disallow `use $crate;`
if source.name == keywords::DollarCrate.name() && module_path.is_empty() {
let crate_root = self.resolve_crate_root(source);
if source.0.name == keywords::DollarCrate.name() && module_path.is_empty() {
let crate_root = self.resolve_crate_root(source.0);
let crate_name = match crate_root.kind {
ModuleKind::Def(_, name) => name,
ModuleKind::Block(..) => unreachable!(),
Expand All @@ -286,11 +286,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
// while the current crate doesn't have a valid `crate_name`.
if crate_name != keywords::Invalid.name() {
// `crate_name` should not be interpreted as relative.
module_path.push(Ident {
module_path.push((Ident {
name: keywords::CrateRoot.name(),
span: source.span,
});
source.name = crate_name;
span: source.0.span,
}, Some(self.session.next_node_id())));
source.0.name = crate_name;
}
if rename.is_none() {
ident.name = crate_name;
Expand All @@ -311,7 +311,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

let subclass = SingleImport {
target: ident,
source,
source: source.0,
result: PerNS {
type_ns: Cell::new(Err(Undetermined)),
value_ns: Cell::new(Err(Undetermined)),
Expand Down Expand Up @@ -349,13 +349,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
);
}
ast::UseTreeKind::Nested(ref items) => {
let prefix = ast::Path {
segments: module_path.into_iter()
.map(|ident| ast::PathSegment::from_ident(ident))
.collect(),
span: path.span,
};

// Ensure there is at most one `self` in the list
let self_spans = items.iter().filter_map(|&(ref use_tree, _)| {
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
Expand All @@ -379,6 +372,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
}

for &(ref tree, id) in items {
let prefix = ast::Path {
segments: module_path.iter()
.map(|ident| {
let mut seg = ast::PathSegment::from_ident(ident.0);
seg.id = self.session.next_node_id();
seg
})
.collect(),
span: path.span,
};

self.build_reduced_graph_for_use_tree(
root_use_tree,
root_id,
Expand Down
Loading