Skip to content

Commit 090be5e

Browse files
committed
Replacing self overwriting with proper resolution
1 parent 4c42051 commit 090be5e

2 files changed

Lines changed: 42 additions & 34 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -626,42 +626,42 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
626626

627627
match use_tree.kind {
628628
ast::UseTreeKind::Simple(rename) => {
629-
let mut ident = use_tree.ident();
630629
let mut module_path = prefix;
631-
let mut source = module_path.pop().unwrap();
630+
let source = module_path.pop().unwrap();
632631

633632
// `true` for `...::{self [as target]}` imports, `false` otherwise.
634633
let type_ns_only = nested && source.ident.name == kw::SelfLower;
635634

635+
// Suggest `use prefix::{self};` for `use prefix::self;`
636636
if source.ident.name == kw::SelfLower
637-
&& let Some(parent) = module_path.pop()
637+
&& let Some(parent) = module_path.last()
638+
&& !type_ns_only
639+
&& (parent.ident.name != kw::PathRoot
640+
|| self.r.path_root_is_crate_root(parent.ident))
638641
{
639-
// Suggest `use prefix::{self};` for `use prefix::self;`
640-
if !type_ns_only
641-
&& (parent.ident.name != kw::PathRoot
642-
|| self.r.path_root_is_crate_root(parent.ident))
643-
{
644-
let span_with_rename = match rename {
645-
Some(rename) => source.ident.span.to(rename.span),
646-
None => source.ident.span,
647-
};
648-
649-
self.r.report_error(
650-
parent.ident.span.shrink_to_hi().to(source.ident.span),
651-
ResolutionError::SelfImportsOnlyAllowedWithin {
652-
root: parent.ident.name == kw::PathRoot,
653-
span_with_rename,
654-
},
655-
);
656-
}
642+
let span_with_rename = match rename {
643+
Some(rename) => source.ident.span.to(rename.span),
644+
None => source.ident.span,
645+
};
657646

658-
let self_span = source.ident.span;
659-
source = parent;
660-
if rename.is_none() {
661-
ident = Ident::new(source.ident.name, self_span);
662-
}
647+
self.r.report_error(
648+
parent.ident.span.shrink_to_hi().to(source.ident.span),
649+
ResolutionError::SelfImportsOnlyAllowedWithin {
650+
root: parent.ident.name == kw::PathRoot,
651+
span_with_rename,
652+
},
653+
);
663654
}
664655

656+
let ident = if source.ident.name == kw::SelfLower
657+
&& rename.is_none()
658+
&& let Some(parent) = module_path.last()
659+
{
660+
Ident::new(parent.ident.name, source.ident.span)
661+
} else {
662+
use_tree.ident()
663+
};
664+
665665
match source.ident.name {
666666
kw::DollarCrate => {
667667
if !module_path.is_empty() {
@@ -698,7 +698,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
698698
}
699699
}
700700
// Deny `use ::{self};` after edition 2015
701-
kw::PathRoot if !self.r.path_root_is_crate_root(source.ident) => {
701+
kw::SelfLower
702+
if let Some(parent) = module_path.last()
703+
&& parent.ident.name == kw::PathRoot
704+
&& !self.r.path_root_is_crate_root(parent.ident) =>
705+
{
702706
self.r.dcx().span_err(use_tree.span(), "extern prelude cannot be imported");
703707
return;
704708
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -959,12 +959,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
959959
) -> Result<Decl<'ra>, Determinacy> {
960960
match module {
961961
ModuleOrUniformRoot::Module(module) => {
962-
if ns == TypeNS
963-
&& ident.name == kw::Super
964-
&& let Some(module) =
965-
self.resolve_super_in_module(ident, Some(module), parent_scope)
966-
{
967-
return Ok(module.self_decl.unwrap());
962+
if ns == TypeNS {
963+
if ident.name == kw::SelfLower {
964+
return Ok(module.self_decl.unwrap());
965+
}
966+
if ident.name == kw::Super
967+
&& let Some(module) =
968+
self.resolve_super_in_module(ident, Some(module), parent_scope)
969+
{
970+
return Ok(module.self_decl.unwrap());
971+
}
968972
}
969973

970974
let (ident_key, def) = IdentKey::new_adjusted(ident, module.expansion);
@@ -1032,7 +1036,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10321036
{
10331037
let module = self.resolve_crate_root(ident);
10341038
return Ok(module.self_decl.unwrap());
1035-
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
1039+
} else if ident.name == kw::Super {
10361040
// FIXME: Implement these with renaming requirements so that e.g.
10371041
// `use super;` doesn't work, but `use super as name;` does.
10381042
// Fall through here to get an error from `early_resolve_...`.

0 commit comments

Comments
 (0)