Skip to content

Commit bae4968

Browse files
committed
Auto merge of rust-lang#120609 - petrochenkov:nousestem2, r=<try>
hir: Stop keeping prefixes for most of `use` list stems And make sure all other imports have non-empty resolution lists. Addresses one of FIXMEs in rust-lang#120206.
2 parents bf3c6c5 + f5d6eb3 commit bae4968

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

compiler/rustc_ast_lowering/src/item.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
496496
}
497497
}
498498

499-
let res =
500-
self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect();
499+
let res = self.lower_import_res(id, path.span);
501500
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
502501
hir::ItemKind::Use(path, hir::UseKind::Single)
503502
}
@@ -533,7 +532,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
533532
// for that we return the `{}` import (called the
534533
// `ListStem`).
535534

536-
let prefix = Path { segments, span: prefix.span.to(path.span), tokens: None };
535+
let span = prefix.span.to(path.span);
536+
let prefix = Path { segments, span, tokens: None };
537537

538538
// Add all the nested `PathListItem`s to the HIR.
539539
for &(ref use_tree, id) in trees {
@@ -567,9 +567,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
567567
});
568568
}
569569

570-
let res =
571-
self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect();
572-
let path = self.lower_use_path(res, &prefix, ParamMode::Explicit);
570+
let path = if trees.is_empty() && !prefix.segments.is_empty() {
571+
// For empty lists we need to lower the prefix so it is checked for things
572+
// like stability later.
573+
let res = self.lower_import_res(id, span);
574+
self.lower_use_path(res, &prefix, ParamMode::Explicit)
575+
} else {
576+
// For non-empty lists we can just drop all the data, the prefix is already
577+
// present in HIR as a part of nested imports.
578+
self.arena.alloc(hir::UsePath { res: smallvec![], segments: &[], span })
579+
};
573580
hir::ItemKind::Use(path, hir::UseKind::ListStem)
574581
}
575582
}

compiler/rustc_ast_lowering/src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6363
use rustc_session::parse::{add_feature_diagnostics, feature_err};
6464
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6565
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
66-
use smallvec::SmallVec;
66+
use smallvec::{smallvec, SmallVec};
6767
use std::collections::hash_map::Entry;
6868
use thin_vec::ThinVec;
6969

@@ -749,8 +749,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
749749
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res())
750750
}
751751

752-
fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
753-
self.resolver.get_import_res(id).present_items()
752+
fn lower_import_res(&mut self, id: NodeId, span: Span) -> SmallVec<[Res; 3]> {
753+
let res = self.resolver.get_import_res(id).present_items();
754+
let res: SmallVec<_> = res.map(|res| self.lower_res(res)).collect();
755+
if res.is_empty() {
756+
self.dcx().span_delayed_bug(span, "no resolution for an import");
757+
return smallvec![Res::Err];
758+
}
759+
res
754760
}
755761

756762
fn make_lang_item_qpath(&mut self, lang_item: hir::LangItem, span: Span) -> hir::QPath<'hir> {

compiler/rustc_ast_lowering/src/path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
156156
p: &Path,
157157
param_mode: ParamMode,
158158
) -> &'hir hir::UsePath<'hir> {
159+
assert!((1..=3).contains(&res.len()));
159160
self.arena.alloc(hir::UsePath {
160161
res,
161162
segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| {

0 commit comments

Comments
 (0)