Skip to content

Commit 3ea33a4

Browse files
committed
WIP: rewrite build_reduced_graph_for_use_tree
1 parent 11d2046 commit 3ea33a4

File tree

8 files changed

+652
-50
lines changed

8 files changed

+652
-50
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ resolve_unexpected_res_use_at_op_in_slice_pat_with_range_sugg =
463463
resolve_unnamed_crate_root_import =
464464
crate root imports need to be explicitly named: `use crate as name;`
465465
466+
resolve_unnamed_imports =
467+
imports need to be explicitly named: `use {$ident} as name;`
468+
466469
resolve_unreachable_label =
467470
use of unreachable label `{$name}`
468471
.label = unreachable label `{$name}`

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -596,65 +596,87 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
596596
}
597597
}
598598
} 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;`
622-
if let Some(parent) = module_path.pop() {
623-
source = parent;
624-
if rename.is_none() {
625-
ident = source.ident;
599+
match source.ident.name {
600+
kw::Crate => {
601+
if !module_path.is_empty() {
602+
self.r.dcx().span_err(
603+
ident.span,
604+
"`crate` in paths can only be used in start position",
605+
);
606+
return;
626607
}
627608
}
628-
}
629-
630-
// Disallow `use $crate;`
631-
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
632-
let crate_root = self.r.resolve_crate_root(source.ident);
633-
let crate_name = match crate_root.kind {
634-
ModuleKind::Def(.., name) => name,
635-
ModuleKind::Block => unreachable!(),
636-
};
637-
// HACK(eddyb) unclear how good this is, but keeping `$crate`
638-
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
639-
// while the current crate doesn't have a valid `crate_name`.
640-
if let Some(crate_name) = crate_name {
641-
// `crate_name` should not be interpreted as relative.
642-
module_path.push(Segment::from_ident_and_id(
643-
Ident::new(kw::PathRoot, source.ident.span),
644-
self.r.next_node_id(),
645-
));
646-
source.ident.name = crate_name;
609+
kw::Super => {
610+
type_ns_only = true;
647611
}
648-
if rename.is_none() {
649-
ident.name = sym::dummy;
612+
kw::SelfLower => {
613+
if let Some(parent) = module_path.pop() {
614+
let span_with_rename = match rename {
615+
Some(rename) => source.ident.span.to(rename.span),
616+
None => source.ident.span,
617+
};
618+
619+
if parent.ident.name != kw::PathRoot {
620+
self.r.report_error(
621+
parent.ident.span.shrink_to_hi().to(source.ident.span),
622+
ResolutionError::SelfImportsOnlyAllowedWithin {
623+
root: false,
624+
span_with_rename,
625+
},
626+
);
627+
}
628+
629+
let self_span = source.ident.span;
630+
source = parent;
631+
if rename.is_none() {
632+
ident = Ident::new(source.ident.name, self_span);
633+
}
634+
}
635+
636+
type_ns_only = true;
650637
}
638+
kw::DollarCrate => {
639+
if !module_path.is_empty() {
640+
self.r.dcx().span_err(
641+
ident.span,
642+
"`$crate` in paths can only be used in start position",
643+
);
644+
return;
645+
}
651646

652-
self.r.dcx().emit_err(errors::CrateImported { span: item.span });
647+
let crate_root = self.r.resolve_crate_root(source.ident);
648+
let crate_name = match crate_root.kind {
649+
ModuleKind::Def(.., name) => name,
650+
ModuleKind::Block => unreachable!(),
651+
};
652+
// HACK(eddyb) unclear how good this is, but keeping `$crate`
653+
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
654+
// while the current crate doesn't have a valid `crate_name`.
655+
if let Some(crate_name) = crate_name {
656+
// `crate_name` should not be interpreted as relative.
657+
module_path.push(Segment::from_ident_and_id(
658+
Ident::new(kw::PathRoot, source.ident.span),
659+
self.r.next_node_id(),
660+
));
661+
source.ident.name = crate_name;
662+
}
663+
}
664+
_ => {}
653665
}
654666
}
655667

656668
if ident.name == kw::Crate {
657669
self.r.dcx().emit_err(errors::UnnamedCrateRootImport { span: ident.span });
670+
return;
671+
} else if ident.name == kw::DollarCrate {
672+
self.r.dcx().emit_err(errors::CrateImported { span: item.span });
673+
return;
674+
} else if source.ident.name == kw::PathRoot {
675+
self.r.dcx().span_err(ident.span, "{{root}} cannot be imported");
676+
return;
677+
} else if ident.is_path_segment_keyword() {
678+
self.r.dcx().emit_err(errors::UnnamedImports { span: ident.span, ident });
679+
return;
658680
}
659681

660682
let kind = ImportKind::Single {
@@ -708,6 +730,12 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
708730
}
709731

710732
e.emit();
733+
} else if let &[self_span] = &self_spans[..]
734+
&& prefix.len() == 1
735+
&& prefix[0].ident.name == kw::DollarCrate
736+
{
737+
// Disallow `use $crate::{self};`
738+
self.r.dcx().emit_err(errors::CrateImported { span: self_span });
711739
}
712740

713741
for &(ref tree, id) in items {

compiler/rustc_resolve/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,14 @@ pub(crate) struct UnnamedCrateRootImport {
898898
pub(crate) span: Span,
899899
}
900900

901+
#[derive(Diagnostic)]
902+
#[diag(resolve_unnamed_imports)]
903+
pub(crate) struct UnnamedImports {
904+
#[primary_span]
905+
pub(crate) span: Span,
906+
pub(crate) ident: Ident,
907+
}
908+
901909
#[derive(Diagnostic)]
902910
#[diag(resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments)]
903911
pub(crate) struct MacroExpandedExternCrateCannotShadowExternArguments {

compiler/rustc_resolve/src/ident.rs

Lines changed: 19 additions & 1 deletion
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(
@@ -869,6 +879,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
869879
// FIXME: Implement these with renaming requirements so that e.g.
870880
// `use super;` doesn't work, but `use super as name;` does.
871881
// Fall through here to get an error from `early_resolve_...`.
882+
883+
if ident.name == kw::Super {
884+
if let Some(parent) = parent_scope.module.parent {
885+
return Ok(parent.self_binding.unwrap());
886+
}
887+
} else {
888+
return Ok(parent_scope.module.self_binding.unwrap());
889+
}
872890
}
873891
}
874892

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
macro_rules! foo {
2+
() => {
3+
use $crate::{self}; //~ ERROR `$crate` may not be imported
4+
};
5+
}
6+
7+
foo!();
8+
9+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: `$crate` may not be imported
2+
--> $DIR/use-dollar-crate-self.rs:3:22
3+
|
4+
LL | use $crate::{self};
5+
| ^^^^
6+
...
7+
LL | foo!();
8+
| ------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//@ edition: 2021
2+
3+
macro_rules! macro_dollar_crate {
4+
($m: ident) => {
5+
pub use $crate as _dollar_crate; // Good
6+
use $crate; //~ ERROR `$crate` may not be imported
7+
use ::$crate; //~ ERROR `$crate` in paths can only be used in start position
8+
use $m::$crate; //~ ERROR `$crate` in paths can only be used in start position
9+
use crate::$crate; //~ ERROR `$crate` in paths can only be used in start position
10+
use super::$crate; //~ ERROR `$crate` in paths can only be used in start position
11+
use self::$crate; //~ ERROR `$crate` in paths can only be used in start position
12+
use $crate::$crate; //~ ERROR `$crate` in paths can only be used in start position
13+
use ::$crate as _dollar_crate2; //~ ERROR `$crate` in paths can only be used in start position
14+
use $m::$crate as _dollar_crate3; //~ ERROR `$crate` in paths can only be used in start position
15+
use crate::$crate as _dollar_crate4; //~ ERROR `$crate` in paths can only be used in start position
16+
use super::$crate as _dollar_crate5; //~ ERROR `$crate` in paths can only be used in start position
17+
use self::$crate as _dollar_crate6; //~ ERROR `$crate` in paths can only be used in start position
18+
use $crate::$crate as _dollar_crate7; //~ ERROR `$crate` in paths can only be used in start position
19+
}
20+
}
21+
22+
fn outer() {}
23+
24+
mod foo {
25+
pub mod bar {
26+
pub mod foobar {
27+
pub mod qux {
28+
pub use super::inner;
29+
}
30+
31+
pub fn inner() {}
32+
}
33+
34+
macro_dollar_crate!(foobar);
35+
36+
pub use crate as _crate; // Good
37+
use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
38+
use ::crate; //~ ERROR `crate` in paths can only be used in start position
39+
use foobar::crate; //~ ERROR `crate` in paths can only be used in start position
40+
use crate::crate; //~ ERROR `crate` in paths can only be used in start position
41+
use super::crate; //~ ERROR `crate` in paths can only be used in start position
42+
use self::crate; //~ ERROR `crate` in paths can only be used in start position
43+
use ::crate as _crate2; //~ ERROR `crate` in paths can only be used in start position
44+
use foobar::crate as _crate3; //~ ERROR `crate` in paths can only be used in start position
45+
use crate::crate as _crate4; //~ ERROR `crate` in paths can only be used in start position
46+
use super::crate as _crate5; //~ ERROR `crate` in paths can only be used in start position
47+
use self::crate as _crate6; //~ ERROR `crate` in paths can only be used in start position
48+
49+
pub use super as _super; // Good
50+
pub use foobar::super as _super3; // Good
51+
pub use super::super as _super5; // Good
52+
pub use self::super as _super6; // Good
53+
use super; //~ ERROR imports need to be explicitly named: `use super as name;`
54+
use ::super; //~ ERROR imports need to be explicitly named: `use super as name;`
55+
use foobar::super; //~ ERROR imports need to be explicitly named: `use super as name;`
56+
use crate::super; //~ ERROR imports need to be explicitly named: `use super as name;`
57+
use super::super; //~ ERROR imports need to be explicitly named: `use super as name;`
58+
use self::super; //~ ERROR imports need to be explicitly named: `use super as name;`
59+
use ::super as _super2; //~ ERROR unresolved import `super`
60+
use crate::super as _super4; //~ ERROR unresolved import `crate::super`
61+
62+
pub use self as _self; // Good
63+
use self; //~ ERROR imports need to be explicitly named: `use self as name;`
64+
use ::self; //~ ERROR {{root}} cannot be imported
65+
pub use foobar::qux::self; //~ ERROR `self` imports are only allowed within a { } list
66+
use crate::self; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
67+
//~^ ERROR `self` imports are only allowed within a { } list
68+
use super::self; //~ ERROR imports need to be explicitly named: `use super as name;`
69+
//~^ ERROR `self` imports are only allowed within a { } list
70+
use self::self; //~ ERROR imports need to be explicitly named: `use self as name;`
71+
//~^ ERROR `self` imports are only allowed within a { } list
72+
use ::self as _self2; //~ ERROR {{root}} cannot be imported
73+
pub use foobar::self as _self3; //~ ERROR `self` imports are only allowed within a { } list
74+
pub use crate::self as _self4; //~ ERROR `self` imports are only allowed within a { } list
75+
pub use super::self as _self5; //~ ERROR `self` imports are only allowed within a { } list
76+
pub use self::self as _self6; //~ ERROR `self` imports are only allowed within a { } list
77+
}
78+
}
79+
80+
fn main() {
81+
foo::bar::_dollar_crate::outer();
82+
83+
foo::bar::_crate::outer();
84+
foo::bar::_crate::foo::bar::foobar::inner();
85+
86+
foo::bar::_super::bar::foobar::inner();
87+
foo::bar::_super3::foobar::qux::inner();
88+
foo::bar::_super5::outer();
89+
foo::bar::_super6::bar::foobar::inner();
90+
91+
foo::bar::_self::foobar::inner();
92+
// Belows work after recovery
93+
foo::bar::qux::inner();
94+
foo::bar::_self3::inner();
95+
foo::bar::_self4::outer();
96+
foo::bar::_self5::bar::foobar::inner();
97+
foo::bar::_self6::foobar::inner();
98+
}

0 commit comments

Comments
 (0)