Skip to content
Open
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
16 changes: 14 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3337,7 +3337,7 @@ pub enum UseTreeKind {
/// ```
Nested { items: ThinVec<(UseTree, NodeId)>, span: Span },
/// `use prefix::*`
Glob,
Glob(Span),
}

/// A tree of paths sharing common prefixes.
Expand All @@ -3346,7 +3346,6 @@ pub enum UseTreeKind {
pub struct UseTree {
pub prefix: Path,
pub kind: UseTreeKind,
pub span: Span,
}

impl UseTree {
Expand All @@ -3359,6 +3358,19 @@ impl UseTree {
_ => panic!("`UseTree::ident` can only be used on a simple import"),
}
}

pub fn span(&self) -> Span {
self.prefix.span.to(self.hi_span())
}

pub fn hi_span(&self) -> Span {
match self.kind {
UseTreeKind::Simple(None) => self.prefix.span,
UseTreeKind::Simple(Some(name)) => name.span,
UseTreeKind::Nested { span, .. } => span,
UseTreeKind::Glob(span) => span,
}
}
}

/// Distinguishes between `Attribute`s that decorate items and Attributes that
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_item_id_use_tree(nested, vec);
}
}
UseTreeKind::Simple(..) | UseTreeKind::Glob => {}
UseTreeKind::Simple(..) | UseTreeKind::Glob(_) => {}
}
}

Expand Down Expand Up @@ -264,7 +264,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ItemKind::Use(use_tree) => {
// Start with an empty prefix.
let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
let prefix = Path {
segments: ThinVec::new(),
span: use_tree.prefix.span.shrink_to_lo(),
tokens: None,
};

self.lower_use_tree(use_tree, &prefix, id, vis_span, attrs)
}
Expand Down Expand Up @@ -635,7 +639,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ident = self.lower_ident(ident);
hir::ItemKind::Use(path, hir::UseKind::Single(ident))
}
UseTreeKind::Glob => {
UseTreeKind::Glob(_) => {
let res = self.expect_full_res(id);
let res = self.lower_res(res);
// Put the result in the appropriate namespace.
Expand Down Expand Up @@ -707,7 +711,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
owner_id,
kind,
vis_span,
span: this.lower_span(use_tree.span),
span: this.lower_span(use_tree.span()),
has_delayed_lints: !this.delayed_lints.is_empty(),
eii: find_attr!(attrs, EiiImpls(..) | EiiDeclaration(..)),
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ impl<'a> State<'a> {
self.print_ident(rename);
}
}
ast::UseTreeKind::Glob => {
ast::UseTreeKind::Glob(_) => {
if !tree.prefix.segments.is_empty() {
self.print_path(&tree.prefix, false, 0);
self.word("::");
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl<'cx, 'a> Context<'cx, 'a> {
UseTree {
prefix: this.cx.path(this.span, vec![Ident::with_dummy_span(sym)]),
kind: UseTreeKind::Simple(None),
span: this.span,
},
DUMMY_NODE_ID,
)
Expand All @@ -121,7 +120,6 @@ impl<'cx, 'a> Context<'cx, 'a> {
],
span: self.span,
},
span: self.span,
}),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ pub fn inject(
thin_vec![cx.attr_word(sym::prelude_import, span)],
ast::ItemKind::Use(ast::UseTree {
prefix: cx.path(span, import_path),
kind: ast::UseTreeKind::Glob,
span,
kind: ast::UseTreeKind::Glob(span),
}),
);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ impl InvocationCollectorNode for Box<ast::Item> {
if let ItemKind::Use(ut) = &self.kind {
fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec<Ident>) {
match &ut.kind {
ast::UseTreeKind::Glob => {}
ast::UseTreeKind::Glob(_) => {}
ast::UseTreeKind::Simple(_) => idents.push(ut.ident()),
ast::UseTreeKind::Nested { items, .. } => {
for (ut, _) in items {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ impl UnusedImportBraces {
}
rename.unwrap_or(orig_ident).name
}
ast::UseTreeKind::Glob => sym::asterisk,
ast::UseTreeKind::Glob(_) => sym::asterisk,
ast::UseTreeKind::Nested { .. } => return,
};

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> Parser<'a> {
let tree = self.parse_use_tree()?;
if let Err(mut e) = self.expect_semi() {
match tree.kind {
UseTreeKind::Glob => {
UseTreeKind::Glob(_) => {
e.note("the wildcard token must be last on the path");
}
UseTreeKind::Nested { .. } => {
Expand Down Expand Up @@ -1303,13 +1303,13 @@ impl<'a> Parser<'a> {
}
};

Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
Ok(UseTree { prefix, kind })
}

/// Parses `*` or `{...}`.
fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
Ok(if self.eat(exp!(Star)) {
UseTreeKind::Glob
UseTreeKind::Glob(self.prev_token.span)
} else {
let lo = self.token.span;
UseTreeKind::Nested {
Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
// so prefixes are prepended with crate root segment if necessary.
// The root is prepended lazily, when the first non-empty prefix or terminating glob
// appears, so imports in braced groups can have roots prepended independently.
let is_glob = matches!(use_tree.kind, ast::UseTreeKind::Glob);
let crate_root = match prefix_iter.peek() {
Some(seg) if !seg.ident.is_path_segment_keyword() && seg.ident.span.is_rust_2015() => {
Some(seg.ident.span.ctxt())
}
None if is_glob && use_tree.span.is_rust_2015() => Some(use_tree.span.ctxt()),
None if let ast::UseTreeKind::Glob(span) = use_tree.kind && span.is_rust_2015() => Some(span.ctxt()),
_ => None,
}
.map(|ctxt| {
Expand Down Expand Up @@ -695,7 +694,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}
// Deny `use ::{self};` after edition 2015
kw::PathRoot if !self.r.path_root_is_crate_root(source.ident) => {
self.r.dcx().span_err(use_tree.span, "extern prelude cannot be imported");
self.r.dcx().span_err(use_tree.span(), "extern prelude cannot be imported");
return;
}
_ => {}
Expand Down Expand Up @@ -726,20 +725,20 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
id,
};

self.add_import(module_path, kind, use_tree.span, item, root_span, item.id, vis);
self.add_import(module_path, kind, use_tree.span(), item, root_span, item.id, vis);
}
ast::UseTreeKind::Glob => {
ast::UseTreeKind::Glob(_) => {
if !ast::attr::contains_name(&item.attrs, sym::prelude_import) {
let kind = ImportKind::Glob { max_vis: CmCell::new(None), id };
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
self.add_import(prefix, kind, use_tree.span(), item, root_span, item.id, vis);
} else {
// Resolve the prelude import early.
let path_res =
self.r.cm().maybe_resolve_path(&prefix, None, &self.parent_scope, None);
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = path_res {
self.r.prelude = Some(module);
} else {
self.r.dcx().span_err(use_tree.span, "cannot resolve a prelude import");
self.r.dcx().span_err(use_tree.span(), "cannot resolve a prelude import");
}
}
}
Expand All @@ -763,7 +762,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
let tree = ast::UseTree {
prefix: ast::Path::from_ident(Ident::new(kw::SelfLower, new_span)),
kind: ast::UseTreeKind::Simple(Some(Ident::new(kw::Underscore, new_span))),
span: use_tree.span,
};
self.build_reduced_graph_for_use_tree(
// This particular use tree
Expand Down Expand Up @@ -834,7 +832,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
// The whole `use` item
item,
vis,
use_tree.span,
use_tree.span(),
);
}

Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,22 @@ fn calc_unused_spans(
) -> UnusedSpanResult {
// The full span is the whole item's span if this current tree is not nested inside another
// This tells rustfix to remove the whole item if all the imports are unused
let full_span = if unused_import.use_tree.span == use_tree.span {
let full_span = if unused_import.use_tree.span() == use_tree.span() {
unused_import.item_span
} else {
use_tree.span
use_tree.span()
};
match use_tree.kind {
ast::UseTreeKind::Simple(..) | ast::UseTreeKind::Glob => {
ast::UseTreeKind::Simple(..) | ast::UseTreeKind::Glob(_) => {
if unused_import.unused.contains(&use_tree_id) {
UnusedSpanResult::Unused { spans: vec![use_tree.span], remove: full_span }
UnusedSpanResult::Unused { spans: vec![use_tree.span()], remove: full_span }
} else {
UnusedSpanResult::Used
}
}
ast::UseTreeKind::Nested { items: ref nested, span: tree_span } => {
if nested.is_empty() {
return UnusedSpanResult::Unused { spans: vec![use_tree.span], remove: full_span };
return UnusedSpanResult::Unused { spans: vec![use_tree.span()], remove: full_span };
}

let mut unused_spans = Vec::new();
Expand Down Expand Up @@ -340,10 +340,10 @@ fn calc_unused_spans(
} else if pos == nested.len() - 1 || used_children > 0 {
// Delete everything from the end of the last import, to delete the
// previous comma
nested[pos - 1].0.span.shrink_to_hi().to(use_tree.span)
nested[pos - 1].0.hi_span().shrink_to_hi().to(use_tree.hi_span())
} else {
// Delete everything until the next import, to delete the trailing commas
use_tree.span.to(nested[pos + 1].0.span.shrink_to_lo())
use_tree.prefix.span.to(nested[pos + 1].0.prefix.span.shrink_to_lo())
};

// Try to collapse adjacent spans into a single one. This prevents all cases of
Expand Down Expand Up @@ -379,11 +379,11 @@ fn calc_unused_spans(
if used_children == 1 && !contains_self {
// Left brace, from the start of the nested group to the first item.
to_remove.push(
tree_span.shrink_to_lo().to(nested.first().unwrap().0.span.shrink_to_lo()),
tree_span.shrink_to_lo().to(nested.first().unwrap().0.prefix.span.shrink_to_lo()),
);
// Right brace, from the end of the last item to the end of the nested group.
to_remove.push(
nested.last().unwrap().0.span.shrink_to_hi().to(tree_span.shrink_to_hi()),
nested.last().unwrap().0.hi_span().shrink_to_hi().to(tree_span.shrink_to_hi()),
);
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
DefKind::Macro(macro_kinds)
}
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
ItemKind::Use(use_tree) => {
self.create_def(i.id, None, DefKind::Use, use_tree.span);
ItemKind::Use(_) => {
self.create_def(i.id, None, DefKind::Use, i.span);
return visit::walk_item(self, i);
}
ItemKind::MacCall(..) | ItemKind::DelegationMac(..) => {
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
}

fn visit_nested_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId) {
self.create_def(id, None, DefKind::Use, use_tree.span);
self.create_def(id, None, DefKind::Use, use_tree.span());
visit::walk_use_tree(self, use_tree);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2949,7 +2949,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {

ItemKind::Use(use_tree) => {
let maybe_exported = match use_tree.kind {
UseTreeKind::Simple(_) | UseTreeKind::Glob => MaybeExported::Ok(item.id),
UseTreeKind::Simple(_) | UseTreeKind::Glob(_) => MaybeExported::Ok(item.id),
UseTreeKind::Nested { .. } => MaybeExported::NestedUse(&item.vis),
};
self.resolve_doc_links(&item.attrs, maybe_exported);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl SingleComponentPathImports {
if !macros.contains(&name) {
single_use_usages.push(SingleUse {
name,
span: tree.0.span,
span: tree.0.span(),
item_id: item.id,
can_suggest: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
.ident;
unsafe_to_safe_check(old_name, new_name, cx, span);
},
UseTreeKind::Simple(None) | UseTreeKind::Glob => {},
UseTreeKind::Simple(None) | UseTreeKind::Glob(_) => {},
UseTreeKind::Nested { ref items, .. } => {
for (use_tree, _) in items {
check_use_tree(use_tree, cx, span);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/ast_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ pub fn eq_const_item_rhs(l: &ConstItemRhsKind, r: &ConstItemRhsKind) -> bool {
pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
use UseTreeKind::*;
match (l, r) {
(Glob, Glob) => true,
(Glob(_), Glob(_)) => true,
(Simple(l), Simple(r)) => both(l.as_ref(), r.as_ref(), |l, r| eq_id(*l, *r)),
(Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-28388-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0432]: unresolved import `foo`
--> $DIR/issue-28388-1.rs:4:5
|
LL | use foo::{};
| ^^^^^^^ no `foo` in the root
| ^^^ no `foo` in the root

error: aborting due to 1 previous error

Expand Down
16 changes: 8 additions & 8 deletions tests/ui/proc-macro/mixed-site-span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const _: () = {
// Failed resolutions of `proc_macro_item`
const _: () = {
// token_site_span::proc_macro_item
invoke_with_crate!{input proc_macro_item} //~ ERROR unresolved import `$crate`
invoke_with_ident!{input proc_macro_item} //~ ERROR unresolved import `$crate`
invoke_with_crate!{input proc_macro_item} //~ ERROR unresolved import `$crate::proc_macro_item`
invoke_with_ident!{input proc_macro_item} //~ ERROR unresolved import `$crate::proc_macro_item`
invoke_with_crate!{call proc_macro_item} //~ ERROR unresolved import `$crate`
invoke_with_ident!{call proc_macro_item} //~ ERROR unresolved import `$crate`
invoke_with_ident!{hello call proc_macro_item} //~ ERROR unresolved import `$crate`
Expand All @@ -59,8 +59,8 @@ const _: () = {

macro_rules! test {() => {
// crate::proc_macro_item
invoke_with_ident!{$crate input proc_macro_item} //~ ERROR unresolved import `$crate`
with_crate!{$crate input proc_macro_item} //~ ERROR unresolved import `$crate`
invoke_with_ident!{$crate input proc_macro_item} //~ ERROR unresolved import `$crate::proc_macro_item`
with_crate!{$crate input proc_macro_item} //~ ERROR unresolved import `$crate::proc_macro_item`
with_crate!{$crate call proc_macro_item} //~ ERROR unresolved import `$crate`

// token_site_span::proc_macro_item
Expand Down Expand Up @@ -98,8 +98,8 @@ const _: () = {

macro_rules! test {() => {
// crate::TokenItem
invoke_with_ident!{$crate input TokenItem} //~ ERROR unresolved import `$crate`
with_crate!{$crate input TokenItem} //~ ERROR unresolved import `$crate`
invoke_with_ident!{$crate input TokenItem} //~ ERROR unresolved import `$crate::TokenItem`
with_crate!{$crate input TokenItem} //~ ERROR unresolved import `$crate::TokenItem`
with_crate!{$crate call TokenItem} //~ ERROR unresolved import `$crate`

// mixed_site_span::TokenItem
Expand Down Expand Up @@ -128,8 +128,8 @@ const _: () = {
// Failed resolutions of `ItemUse`
const _: () = {
// token_site_span::ItemUse
invoke_with_crate!{input ItemUse} //~ ERROR unresolved import `$crate`
invoke_with_ident!{input ItemUse} //~ ERROR unresolved import `$crate`
invoke_with_crate!{input ItemUse} //~ ERROR unresolved import `$crate::ItemUse`
invoke_with_ident!{input ItemUse} //~ ERROR unresolved import `$crate::ItemUse`

// mixed_site_span::ItemUse
invoke_with_crate!{mixed ItemUse} //~ ERROR unresolved import `$crate`
Expand Down
Loading
Loading