From 50e42ea9f70361ccd71682070c01ea808891f0ce Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 12 Sep 2015 16:10:12 +0300 Subject: Correctly walk import lists in AST visitors --- src/libsyntax/visit.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 8365a7375c6..2a2f5c0f2c1 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -127,6 +127,9 @@ pub trait Visitor<'v> : Sized { fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) { walk_path(self, path) } + fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) { + walk_path_list_item(self, prefix, item) + } fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) { walk_path_segment(self, path_span, path_segment) } @@ -209,33 +212,21 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { ItemExternCrate(..) => {} ItemUse(ref vp) => { match vp.node { - ViewPathSimple(ident, ref path) => { - visitor.visit_ident(vp.span, ident); + ViewPathSimple(_ident, ref path) => { visitor.visit_path(path, item.id); } ViewPathGlob(ref path) => { visitor.visit_path(path, item.id); } ViewPathList(ref prefix, ref list) => { - for id in list { - match id.node { - PathListIdent { name, rename, .. } => { - visitor.visit_ident(id.span, name); - if let Some(ident) = rename { - visitor.visit_ident(id.span, ident); - } - } - PathListMod { rename, .. } => { - if let Some(ident) = rename { - visitor.visit_ident(id.span, ident); - } - } + if !list.is_empty() { + for item in list { + visitor.visit_path_list_item(prefix, item) } + } else { + // FIXME: uncomment this and fix the resulting ICE + // visitor.visit_path(prefix, item.id); } - - // Note that the `prefix` here is not a complete - // path, so we don't use `visit_path`. - walk_path(visitor, prefix); } } } @@ -417,6 +408,17 @@ pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) { } } +pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path, + item: &'v PathListItem) { + for segment in &prefix.segments { + visitor.visit_path_segment(prefix.span, segment); + } + + if let PathListIdent { name, .. } = item.node { + visitor.visit_ident(item.span, name); + } +} + pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, path_span: Span, segment: &'v PathSegment) { -- cgit 1.4.1-3-g733a5 From 357982fae4ac7be0f6e0065399606f2d78618aaf Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 14 Sep 2015 01:24:27 +0300 Subject: Workaround for imports with empty braces --- src/librustc_front/visit.rs | 4 ++-- src/librustc_privacy/lib.rs | 10 +++++++--- src/libstd/lib.rs | 1 - src/libsyntax/visit.rs | 4 ++-- src/test/compile-fail/issue-28075.rs | 5 ++--- 5 files changed, 13 insertions(+), 11 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc_front/visit.rs b/src/librustc_front/visit.rs index 89c9818efbf..12a20a8d21f 100644 --- a/src/librustc_front/visit.rs +++ b/src/librustc_front/visit.rs @@ -218,8 +218,8 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_path_list_item(prefix, item) } } else { - // FIXME: uncomment this and fix the resulting ICE - // visitor.visit_path(prefix, item.id); + // FIXME(#28388) visit_path should be used instead of walk_path + walk_path(visitor, prefix); } } } diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 1a07877066e..817fd984708 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -980,15 +980,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> { } fn visit_path(&mut self, path: &hir::Path, id: ast::NodeId) { - self.check_path(path.span, id, path.segments.last().unwrap().identifier.name); - visit::walk_path(self, path); + if !path.segments.is_empty() { + self.check_path(path.span, id, path.segments.last().unwrap().identifier.name); + visit::walk_path(self, path); + } } fn visit_path_list_item(&mut self, prefix: &hir::Path, item: &hir::PathListItem) { let name = if let hir::PathListIdent { name, .. } = item.node { name.name - } else { + } else if !prefix.segments.is_empty() { prefix.segments.last().unwrap().identifier.name + } else { + self.tcx.sess.bug("`self` import in an import list with empty prefix"); }; self.check_path(item.span, item.node.id(), name); visit::walk_path_list_item(self, prefix, item); diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 6eabf5d69a2..774d13966bd 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -201,7 +201,6 @@ #![feature(alloc)] #![feature(allow_internal_unstable)] -#![feature(arc_weak)] #![feature(associated_consts)] #![feature(borrow_state)] #![feature(box_syntax)] diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 2a2f5c0f2c1..cda750c5cda 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -224,8 +224,8 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_path_list_item(prefix, item) } } else { - // FIXME: uncomment this and fix the resulting ICE - // visitor.visit_path(prefix, item.id); + // FIXME(#28388) visit_path should be used instead of walk_path + walk_path(visitor, prefix); } } } diff --git a/src/test/compile-fail/issue-28075.rs b/src/test/compile-fail/issue-28075.rs index 74a241e819f..7e9dfcf3ccb 100644 --- a/src/test/compile-fail/issue-28075.rs +++ b/src/test/compile-fail/issue-28075.rs @@ -12,11 +12,10 @@ #![allow(unused_imports)] -use std::thread::{catch_panic, sleep}; //~ ERROR use of unstable library feature 'catch_panic' -//~^ ERROR use of unstable library feature 'thread_sleep' +use std::thread::{catch_panic, ScopedKey}; //~ ERROR use of unstable library feature 'catch_panic' +//~^ ERROR use of unstable library feature 'scoped_tls' use std::rt::{self}; //~ ERROR use of unstable library feature 'rt' -use std::rt::{}; fn main() { } -- cgit 1.4.1-3-g733a5 From c3f53d1b12dfe5e08f98b19e9e3980da116e31ee Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 16 Sep 2015 00:50:50 +0300 Subject: Resolve prefix in imports with empty braces --- src/librustc_front/visit.rs | 3 +-- src/librustc_resolve/lib.rs | 28 ++++++++++++++++++++++------ src/libsyntax/visit.rs | 3 +-- src/test/compile-fail/issue-28388-1.rs | 15 +++++++++++++++ src/test/compile-fail/issue-28388-2.rs | 19 +++++++++++++++++++ src/test/compile-fail/issue-28388-3.rs | 16 ++++++++++++++++ 6 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 src/test/compile-fail/issue-28388-1.rs create mode 100644 src/test/compile-fail/issue-28388-2.rs create mode 100644 src/test/compile-fail/issue-28388-3.rs (limited to 'src/libsyntax') diff --git a/src/librustc_front/visit.rs b/src/librustc_front/visit.rs index 12a20a8d21f..8f5208d3ef1 100644 --- a/src/librustc_front/visit.rs +++ b/src/librustc_front/visit.rs @@ -218,8 +218,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_path_list_item(prefix, item) } } else { - // FIXME(#28388) visit_path should be used instead of walk_path - walk_path(visitor, prefix); + visitor.visit_path(prefix, item.id); } } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 8e34118957e..6159ba5b79e 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -2210,10 +2210,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { ItemUse(ref view_path) => { // check for imports shadowing primitive types - let check_rename = |id, ident: Ident| { - match self.def_map.borrow().get(&id).map(|d| d.full_def()) { + let check_rename = |this: &Self, id, ident: Ident| { + match this.def_map.borrow().get(&id).map(|d| d.full_def()) { Some(DefTy(..)) | Some(DefStruct(..)) | Some(DefTrait(..)) | None => { - self.check_if_primitive_type_name(ident.name, item.span); + this.check_if_primitive_type_name(ident.name, item.span); } _ => {} } @@ -2221,12 +2221,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { match view_path.node { hir::ViewPathSimple(ident, _) => { - check_rename(item.id, ident); + check_rename(self, item.id, ident); } - hir::ViewPathList(_, ref items) => { + hir::ViewPathList(ref prefix, ref items) => { for item in items { if let Some(ident) = item.node.rename() { - check_rename(item.node.id(), ident); + check_rename(self, item.node.id(), ident); + } + } + + // Resolve prefix of an import with empty braces (issue #28388) + if items.is_empty() && !prefix.segments.is_empty() { + match self.resolve_crate_relative_path(prefix.span, + &prefix.segments, + TypeNS) { + Some((def, lp)) => self.record_def(item.id, + PathResolution::new(def, lp, 0)), + None => { + resolve_error(self, + prefix.span, + ResolutionError::FailedToResolve( + &path_names_to_string(prefix, 0))); + } } } } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index cda750c5cda..f4f4c9dfc24 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -224,8 +224,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_path_list_item(prefix, item) } } else { - // FIXME(#28388) visit_path should be used instead of walk_path - walk_path(visitor, prefix); + visitor.visit_path(prefix, item.id); } } } diff --git a/src/test/compile-fail/issue-28388-1.rs b/src/test/compile-fail/issue-28388-1.rs new file mode 100644 index 00000000000..ef97b400b00 --- /dev/null +++ b/src/test/compile-fail/issue-28388-1.rs @@ -0,0 +1,15 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc. + +use foo::{}; //~ ERROR failed to resolve. foo + +fn main() {} diff --git a/src/test/compile-fail/issue-28388-2.rs b/src/test/compile-fail/issue-28388-2.rs new file mode 100644 index 00000000000..837dc67c804 --- /dev/null +++ b/src/test/compile-fail/issue-28388-2.rs @@ -0,0 +1,19 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc. + +mod m { + mod n {} +} + +use m::n::{}; //~ ERROR module `n` is private + +fn main() {} diff --git a/src/test/compile-fail/issue-28388-3.rs b/src/test/compile-fail/issue-28388-3.rs new file mode 100644 index 00000000000..59756be55bd --- /dev/null +++ b/src/test/compile-fail/issue-28388-3.rs @@ -0,0 +1,16 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc. + +use std::rt::{}; //~ ERROR use of unstable library feature 'rt' +use std::{}; // OK + +fn main() {} -- cgit 1.4.1-3-g733a5