Skip to content

Commit 62e3a5b

Browse files
committed
Rewrite build_reduced_graph_for_use_tree
1 parent 11d2046 commit 62e3a5b

File tree

8 files changed

+1058
-98
lines changed

8 files changed

+1058
-98
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,6 @@ resolve_remove_surrounding_derive =
373373
374374
resolve_remove_unnecessary_import = remove unnecessary import
375375
376-
resolve_self_import_can_only_appear_once_in_the_list =
377-
`self` import can only appear once in an import list
378-
.label = can only appear once in an import list
379-
380376
resolve_self_import_only_in_import_list_with_non_empty_prefix =
381377
`self` import can only appear in an import list with a non-empty prefix
382378
.label = can only appear in an import list with a non-empty prefix
@@ -463,6 +459,9 @@ resolve_unexpected_res_use_at_op_in_slice_pat_with_range_sugg =
463459
resolve_unnamed_crate_root_import =
464460
crate root imports need to be explicitly named: `use crate as name;`
465461
462+
resolve_unnamed_super_or_self_import = imports need to be explicitly named
463+
resolve_unnamed_super_or_self_sugg_import = try renaming it with a name
464+
466465
resolve_unreachable_label =
467466
use of unreachable label `{$name}`
468467
.label = unreachable label `{$name}`

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -572,63 +572,62 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
572572
let mut ident = use_tree.ident();
573573
let mut module_path = prefix;
574574
let mut source = module_path.pop().unwrap();
575-
let mut type_ns_only = false;
576-
577-
if nested {
578-
// Correctly handle `self`
579-
if source.ident.name == kw::SelfLower {
580-
type_ns_only = true;
581-
582-
if empty_for_self(&module_path) {
575+
let type_ns_only = nested
576+
&& source.ident.name == kw::SelfLower
577+
&& use_tree.prefix.segments.len() == 1;
578+
579+
match source.ident.name {
580+
kw::Crate => {
581+
if !module_path.is_empty() {
582+
self.r.dcx().span_err(
583+
source.ident.span,
584+
"`crate` in paths can only be used in start position",
585+
);
586+
return;
587+
}
588+
}
589+
kw::SelfLower => {
590+
if type_ns_only && empty_for_self(&module_path) {
583591
self.r.report_error(
584592
use_tree.span,
585593
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix,
586594
);
587595
return;
588596
}
589597

590-
// Replace `use foo::{ self };` with `use foo;`
591-
let self_span = source.ident.span;
592-
source = module_path.pop().unwrap();
593-
if rename.is_none() {
594-
// Keep the span of `self`, but the name of `foo`
595-
ident = Ident::new(source.ident.name, self_span);
596-
}
597-
}
598-
} else {
599-
// Disallow `self`
600-
if source.ident.name == kw::SelfLower {
601-
let parent = module_path.last();
602-
603-
let span = match parent {
604-
// only `::self` from `use foo::self as bar`
605-
Some(seg) => seg.ident.span.shrink_to_hi().to(source.ident.span),
606-
None => source.ident.span,
607-
};
608-
let span_with_rename = match rename {
609-
// only `self as bar` from `use foo::self as bar`
610-
Some(rename) => source.ident.span.to(rename.span),
611-
None => source.ident.span,
612-
};
613-
self.r.report_error(
614-
span,
615-
ResolutionError::SelfImportsOnlyAllowedWithin {
616-
root: parent.is_none(),
617-
span_with_rename,
618-
},
619-
);
620-
621-
// Error recovery: replace `use foo::self;` with `use foo;`
622598
if let Some(parent) = module_path.pop() {
599+
let span_with_rename = match rename {
600+
Some(rename) => source.ident.span.to(rename.span),
601+
None => source.ident.span,
602+
};
603+
604+
// Suggest `use prefix::{self};` for `use prefix::self;`
605+
if !type_ns_only && parent.ident.name != kw::PathRoot {
606+
self.r.report_error(
607+
parent.ident.span.shrink_to_hi().to(source.ident.span),
608+
ResolutionError::SelfImportsOnlyAllowedWithin {
609+
root: false,
610+
span_with_rename,
611+
},
612+
);
613+
}
614+
615+
let self_span = source.ident.span;
623616
source = parent;
624617
if rename.is_none() {
625-
ident = source.ident;
618+
ident = Ident::new(source.ident.name, self_span);
626619
}
627620
}
628621
}
622+
kw::DollarCrate => {
623+
if !module_path.is_empty() {
624+
self.r.dcx().span_err(
625+
source.ident.span,
626+
"`$crate` in paths can only be used in start position",
627+
);
628+
return;
629+
}
629630

630-
// Disallow `use $crate;`
631-
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
632631
let crate_root = self.r.resolve_crate_root(source.ident);
633632
let crate_name = match crate_root.kind {
634633
ModuleKind::Def(.., name) => name,
@@ -645,16 +644,53 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
645644
));
646645
source.ident.name = crate_name;
647646
}
648-
if rename.is_none() {
649-
ident.name = sym::dummy;
650-
}
651-
652-
self.r.dcx().emit_err(errors::CrateImported { span: item.span });
653647
}
648+
_ => {}
654649
}
655650

656-
if ident.name == kw::Crate {
657-
self.r.dcx().emit_err(errors::UnnamedCrateRootImport { span: ident.span });
651+
// Deny `use ::{self};`
652+
if source.ident.name == kw::PathRoot {
653+
self.r.dcx().span_err(use_tree.span, "crate root cannot be imported");
654+
return;
655+
}
656+
657+
if rename.is_none() {
658+
match ident.name {
659+
// Deny `use crate;` and `use crate::{self};`
660+
kw::Crate => {
661+
self.r
662+
.dcx()
663+
.emit_err(errors::UnnamedCrateRootImport { span: ident.span });
664+
return;
665+
}
666+
// Deny `use $crate;` and `use $crate::{self};`
667+
kw::DollarCrate => {
668+
self.r.dcx().emit_err(errors::CrateImported { span: ident.span });
669+
return;
670+
}
671+
// Deny `use super;`, `use super::{self};`, `use self;` and `use self::{self};`
672+
kw::Super | kw::SelfLower => {
673+
let ident = use_tree.ident();
674+
675+
// Don't suggest `use super::self as name;` for `use super::self;`
676+
// But it's OK to suggest `use super::{self as name};` for `use super::{self};`
677+
let sugg = if !type_ns_only && ident.name == kw::SelfLower {
678+
None
679+
} else {
680+
Some(errors::UnnamedSuperOrSelfImportSugg {
681+
span: ident.span,
682+
ident,
683+
})
684+
};
685+
686+
self.r.dcx().emit_err(errors::UnnamedSuperOrSelfImport {
687+
span: ident.span,
688+
sugg,
689+
});
690+
return;
691+
}
692+
_ => (),
693+
}
658694
}
659695

660696
let kind = ImportKind::Single {
@@ -684,32 +720,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
684720
}
685721
}
686722
ast::UseTreeKind::Nested { ref items, .. } => {
687-
// Ensure there is at most one `self` in the list
688-
let self_spans = items
689-
.iter()
690-
.filter_map(|(use_tree, _)| {
691-
if let ast::UseTreeKind::Simple(..) = use_tree.kind
692-
&& use_tree.ident().name == kw::SelfLower
693-
{
694-
return Some(use_tree.span);
695-
}
696-
697-
None
698-
})
699-
.collect::<Vec<_>>();
700-
if self_spans.len() > 1 {
701-
let mut e = self.r.into_struct_error(
702-
self_spans[0],
703-
ResolutionError::SelfImportCanOnlyAppearOnceInTheList,
704-
);
705-
706-
for other_span in self_spans.iter().skip(1) {
707-
e.span_label(*other_span, "another `self` import appears here");
708-
}
709-
710-
e.emit();
711-
}
712-
713723
for &(ref tree, id) in items {
714724
self.build_reduced_graph_for_use_tree(
715725
// This particular use tree

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
890890
mpart_suggestion,
891891
})
892892
}
893-
ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
894-
self.dcx().create_err(errs::SelfImportCanOnlyAppearOnceInTheList { span })
895-
}
896893
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
897894
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
898895
}

compiler/rustc_resolve/src/errors.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,6 @@ pub(crate) struct UnreachableLabelWithSimilarNameExists {
204204
pub(crate) ident_span: Span,
205205
}
206206

207-
#[derive(Diagnostic)]
208-
#[diag(resolve_self_import_can_only_appear_once_in_the_list, code = E0430)]
209-
pub(crate) struct SelfImportCanOnlyAppearOnceInTheList {
210-
#[primary_span]
211-
#[label]
212-
pub(crate) span: Span,
213-
}
214-
215207
#[derive(Diagnostic)]
216208
#[diag(resolve_self_import_only_in_import_list_with_non_empty_prefix, code = E0431)]
217209
pub(crate) struct SelfImportOnlyInImportListWithNonEmptyPrefix {
@@ -898,6 +890,27 @@ pub(crate) struct UnnamedCrateRootImport {
898890
pub(crate) span: Span,
899891
}
900892

893+
#[derive(Subdiagnostic)]
894+
#[multipart_suggestion(
895+
resolve_unnamed_super_or_self_sugg_import,
896+
applicability = "maybe-incorrect",
897+
style = "verbose"
898+
)]
899+
pub(crate) struct UnnamedSuperOrSelfImportSugg {
900+
#[suggestion_part(code = "{ident} as name")]
901+
pub(crate) span: Span,
902+
pub(crate) ident: Ident,
903+
}
904+
905+
#[derive(Diagnostic)]
906+
#[diag(resolve_unnamed_super_or_self_import)]
907+
pub(crate) struct UnnamedSuperOrSelfImport {
908+
#[primary_span]
909+
pub(crate) span: Span,
910+
#[subdiagnostic]
911+
pub(crate) sugg: Option<UnnamedSuperOrSelfImportSugg>,
912+
}
913+
901914
#[derive(Diagnostic)]
902915
#[diag(resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments)]
903916
pub(crate) struct MacroExpandedExternCrateCannotShadowExternArguments {

compiler/rustc_resolve/src/ident.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
828828
ignore_import: Option<Import<'ra>>,
829829
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
830830
let module = match module {
831-
ModuleOrUniformRoot::Module(module) => module,
831+
ModuleOrUniformRoot::Module(module) => {
832+
if ns == TypeNS {
833+
if ident.name == kw::Super {
834+
if let Some(parent) = module.parent {
835+
return Ok(parent.self_binding.unwrap());
836+
}
837+
}
838+
}
839+
840+
module
841+
}
832842
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => {
833843
assert_eq!(shadowing, Shadowing::Unrestricted);
834844
let binding = self.resolve_ident_in_scope_set(
@@ -865,10 +875,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
865875
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
866876
let module = self.resolve_crate_root(ident);
867877
return Ok(module.self_binding.unwrap());
868-
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
869-
// FIXME: Implement these with renaming requirements so that e.g.
870-
// `use super;` doesn't work, but `use super as name;` does.
871-
// Fall through here to get an error from `early_resolve_...`.
878+
} else if ident.name == kw::Super {
879+
if let Some(parent) = parent_scope.module.parent {
880+
return Ok(parent.self_binding.unwrap());
881+
}
882+
} else if ident.name == kw::SelfLower {
883+
return Ok(parent_scope.module.self_binding.unwrap());
872884
}
873885
}
874886

compiler/rustc_resolve/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ enum ResolutionError<'ra> {
263263
UndeclaredLabel { name: Symbol, suggestion: Option<LabelSuggestion> },
264264
/// Error E0429: `self` imports are only allowed within a `{ }` list.
265265
SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span },
266-
/// Error E0430: `self` import can only appear once in the list.
267-
SelfImportCanOnlyAppearOnceInTheList,
268266
/// Error E0431: `self` import can only appear in an import list with a non-empty prefix.
269267
SelfImportOnlyInImportListWithNonEmptyPrefix,
270268
/// Error E0433: failed to resolve.

0 commit comments

Comments
 (0)