Skip to content

Commit 3731acc

Browse files
authored
Rollup merge of #120609 - petrochenkov:nousestem2, r=compiler-errors
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 #120206.
2 parents 5a2cec2 + f5d6eb3 commit 3731acc

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
@@ -498,8 +498,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
498498
}
499499
}
500500

501-
let res =
502-
self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect();
501+
let res = self.lower_import_res(id, path.span);
503502
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
504503
hir::ItemKind::Use(path, hir::UseKind::Single)
505504
}
@@ -535,7 +534,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
535534
// for that we return the `{}` import (called the
536535
// `ListStem`).
537536

538-
let prefix = Path { segments, span: prefix.span.to(path.span), tokens: None };
537+
let span = prefix.span.to(path.span);
538+
let prefix = Path { segments, span, tokens: None };
539539

540540
// Add all the nested `PathListItem`s to the HIR.
541541
for &(ref use_tree, id) in trees {
@@ -569,9 +569,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
569569
});
570570
}
571571

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

compiler/rustc_ast_lowering/src/lib.rs

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

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

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

757763
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
@@ -196,6 +196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
196196
p: &Path,
197197
param_mode: ParamMode,
198198
) -> &'hir hir::UsePath<'hir> {
199+
assert!((1..=3).contains(&res.len()));
199200
self.arena.alloc(hir::UsePath {
200201
res,
201202
segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| {

0 commit comments

Comments
 (0)