mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 04:08:40 +00:00
resolve: Cleanup resolve_crate_root
This commit is contained in:
parent
1029775ad5
commit
1328bdeef8
@ -156,7 +156,7 @@ impl<'a> Resolver<'a> {
|
|||||||
|
|
||||||
// Disallow `use $crate;`
|
// Disallow `use $crate;`
|
||||||
if source.name == keywords::DollarCrate.name() && path.segments.len() == 1 {
|
if source.name == keywords::DollarCrate.name() && path.segments.len() == 1 {
|
||||||
let crate_root = self.resolve_crate_root(source.span.ctxt(), true);
|
let crate_root = self.resolve_crate_root(source);
|
||||||
let crate_name = match crate_root.kind {
|
let crate_name = match crate_root.kind {
|
||||||
ModuleKind::Def(_, name) => name,
|
ModuleKind::Def(_, name) => name,
|
||||||
ModuleKind::Block(..) => unreachable!(),
|
ModuleKind::Block(..) => unreachable!(),
|
||||||
|
@ -1981,8 +1981,9 @@ impl<'a> Resolver<'a> {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_crate_root(&mut self, mut ctxt: SyntaxContext, legacy: bool) -> Module<'a> {
|
fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> {
|
||||||
let mark = if legacy {
|
let mut ctxt = ident.span.ctxt();
|
||||||
|
let mark = if ident.name == keywords::DollarCrate.name() {
|
||||||
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
|
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
|
||||||
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
|
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
|
||||||
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
|
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
|
||||||
@ -3345,14 +3346,11 @@ impl<'a> Resolver<'a> {
|
|||||||
if ns == TypeNS {
|
if ns == TypeNS {
|
||||||
if (i == 0 && name == keywords::CrateRoot.name()) ||
|
if (i == 0 && name == keywords::CrateRoot.name()) ||
|
||||||
(i == 0 && name == keywords::Crate.name()) ||
|
(i == 0 && name == keywords::Crate.name()) ||
|
||||||
|
(i == 0 && name == keywords::DollarCrate.name()) ||
|
||||||
(i == 1 && name == keywords::Crate.name() &&
|
(i == 1 && name == keywords::Crate.name() &&
|
||||||
path[0].name == keywords::CrateRoot.name()) {
|
path[0].name == keywords::CrateRoot.name()) {
|
||||||
// `::a::b` or `::crate::a::b`
|
// `::a::b`, `crate::a::b`, `::crate::a::b` or `$crate::a::b`
|
||||||
module = Some(self.resolve_crate_root(ident.span.ctxt(), false));
|
module = Some(self.resolve_crate_root(ident));
|
||||||
continue
|
|
||||||
} else if i == 0 && name == keywords::DollarCrate.name() {
|
|
||||||
// `$crate::a::b`
|
|
||||||
module = Some(self.resolve_crate_root(ident.span.ctxt(), true));
|
|
||||||
continue
|
continue
|
||||||
} else if i == 1 && !ident.is_path_segment_keyword() {
|
} else if i == 1 && !ident.is_path_segment_keyword() {
|
||||||
let prev_name = path[0].name;
|
let prev_name = path[0].name;
|
||||||
|
@ -155,10 +155,9 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let ident = path.segments[0].ident;
|
if path.segments[0].ident.name == keywords::DollarCrate.name() {
|
||||||
if ident.name == keywords::DollarCrate.name() {
|
let module = self.0.resolve_crate_root(path.segments[0].ident);
|
||||||
path.segments[0].ident.name = keywords::CrateRoot.name();
|
path.segments[0].ident.name = keywords::CrateRoot.name();
|
||||||
let module = self.0.resolve_crate_root(ident.span.ctxt(), true);
|
|
||||||
if !module.is_local() {
|
if !module.is_local() {
|
||||||
let span = path.segments[0].ident.span;
|
let span = path.segments[0].ident.span;
|
||||||
path.segments.insert(1, match module.kind {
|
path.segments.insert(1, match module.kind {
|
||||||
|
@ -698,7 +698,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
|
|||||||
"crate root imports need to be explicitly named: \
|
"crate root imports need to be explicitly named: \
|
||||||
`use crate as name;`".to_string()));
|
`use crate as name;`".to_string()));
|
||||||
} else {
|
} else {
|
||||||
Some(self.resolve_crate_root(source.span.ctxt().modern(), false))
|
Some(self.resolve_crate_root(source))
|
||||||
}
|
}
|
||||||
} else if is_extern && !source.is_path_segment_keyword() {
|
} else if is_extern && !source.is_path_segment_keyword() {
|
||||||
let crate_id =
|
let crate_id =
|
||||||
|
Loading…
Reference in New Issue
Block a user