Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
3fbb2549
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3fbb2549
编写于
3月 14, 2020
作者:
V
Vadim Petrochenkov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
resolve: `Legacy(Scope,Binding)` -> `MacroRules(Scope,Binding)`
上级
65bf4831
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
67 addition
and
53 deletion
+67
-53
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/build_reduced_graph.rs
+17
-17
src/librustc_resolve/diagnostics.rs
src/librustc_resolve/diagnostics.rs
+7
-5
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+27
-17
src/librustc_resolve/macros.rs
src/librustc_resolve/macros.rs
+16
-14
未找到文件。
src/librustc_resolve/build_reduced_graph.rs
浏览文件 @
3fbb2549
...
...
@@ -7,7 +7,7 @@
use
crate
::
def_collector
::
collect_definitions
;
use
crate
::
imports
::{
Import
,
ImportKind
};
use
crate
::
macros
::{
LegacyBinding
,
Legacy
Scope
};
use
crate
::
macros
::{
MacroRulesBinding
,
MacroRules
Scope
};
use
crate
::
Namespace
::{
self
,
MacroNS
,
TypeNS
,
ValueNS
};
use
crate
::{
CrateLint
,
Determinacy
,
PathResult
,
ResolutionError
,
VisResolutionError
};
use
crate
::{
...
...
@@ -165,11 +165,11 @@ impl<'a> Resolver<'a> {
&
mut
self
,
fragment
:
&
AstFragment
,
parent_scope
:
ParentScope
<
'a
>
,
)
->
Legacy
Scope
<
'a
>
{
)
->
MacroRules
Scope
<
'a
>
{
collect_definitions
(
&
mut
self
.definitions
,
fragment
,
parent_scope
.expansion
);
let
mut
visitor
=
BuildReducedGraphVisitor
{
r
:
self
,
parent_scope
};
fragment
.visit_with
(
&
mut
visitor
);
visitor
.parent_scope.
legacy
visitor
.parent_scope.
macro_rules
}
crate
fn
build_reduced_graph_external
(
&
mut
self
,
module
:
Module
<
'a
>
)
{
...
...
@@ -1060,7 +1060,7 @@ fn contains_macro_use(&mut self, attrs: &[ast::Attribute]) -> bool {
false
}
fn
visit_invoc
(
&
mut
self
,
id
:
NodeId
)
->
Legacy
Scope
<
'a
>
{
fn
visit_invoc
(
&
mut
self
,
id
:
NodeId
)
->
MacroRules
Scope
<
'a
>
{
let
invoc_id
=
id
.placeholder_to_expn_id
();
self
.parent_scope.module.unexpanded_invocations
.borrow_mut
()
.insert
(
invoc_id
);
...
...
@@ -1068,7 +1068,7 @@ fn visit_invoc(&mut self, id: NodeId) -> LegacyScope<'a> {
let
old_parent_scope
=
self
.r.invocation_parent_scopes
.insert
(
invoc_id
,
self
.parent_scope
);
assert
!
(
old_parent_scope
.is_none
(),
"invocation data is reset for an invocation"
);
Legacy
Scope
::
Invocation
(
invoc_id
)
MacroRules
Scope
::
Invocation
(
invoc_id
)
}
fn
proc_macro_stub
(
item
:
&
ast
::
Item
)
->
Option
<
(
MacroKind
,
Ident
,
Span
)
>
{
...
...
@@ -1095,7 +1095,7 @@ fn insert_unused_macro(&mut self, ident: Ident, node_id: NodeId, span: Span) {
}
}
fn
define_macro
(
&
mut
self
,
item
:
&
ast
::
Item
)
->
Legacy
Scope
<
'a
>
{
fn
define_macro
(
&
mut
self
,
item
:
&
ast
::
Item
)
->
MacroRules
Scope
<
'a
>
{
let
parent_scope
=
self
.parent_scope
;
let
expansion
=
parent_scope
.expansion
;
let
(
ext
,
ident
,
span
,
macro_rules
)
=
match
&
item
.kind
{
...
...
@@ -1108,7 +1108,7 @@ fn define_macro(&mut self, item: &ast::Item) -> LegacyScope<'a> {
self
.r.proc_macro_stubs
.insert
(
item
.id
);
(
self
.r
.dummy_ext
(
macro_kind
),
ident
,
span
,
false
)
}
None
=>
return
parent_scope
.
legacy
,
None
=>
return
parent_scope
.
macro_rules
,
},
_
=>
unreachable!
(),
};
...
...
@@ -1137,8 +1137,8 @@ fn define_macro(&mut self, item: &ast::Item) -> LegacyScope<'a> {
self
.r
.check_reserved_macro_name
(
ident
,
res
);
self
.insert_unused_macro
(
ident
,
item
.id
,
span
);
}
LegacyScope
::
Binding
(
self
.r.arenas
.alloc_legacy_binding
(
Legacy
Binding
{
parent_
legacy_scope
:
parent_scope
.legacy
,
MacroRulesScope
::
Binding
(
self
.r.arenas
.alloc_macro_rules_binding
(
MacroRules
Binding
{
parent_
macro_rules_scope
:
parent_scope
.macro_rules
,
binding
,
ident
,
}))
...
...
@@ -1149,7 +1149,7 @@ fn define_macro(&mut self, item: &ast::Item) -> LegacyScope<'a> {
self
.insert_unused_macro
(
ident
,
item
.id
,
span
);
}
self
.r
.define
(
module
,
ident
,
MacroNS
,
(
res
,
vis
,
span
,
expansion
));
self
.parent_scope.
legacy
self
.parent_scope.
macro_rules
}
}
}
...
...
@@ -1174,29 +1174,29 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
fn
visit_item
(
&
mut
self
,
item
:
&
'b
Item
)
{
let
macro_use
=
match
item
.kind
{
ItemKind
::
MacroDef
(
..
)
=>
{
self
.parent_scope.
legacy
=
self
.define_macro
(
item
);
self
.parent_scope.
macro_rules
=
self
.define_macro
(
item
);
return
;
}
ItemKind
::
MacCall
(
..
)
=>
{
self
.parent_scope.
legacy
=
self
.visit_invoc
(
item
.id
);
self
.parent_scope.
macro_rules
=
self
.visit_invoc
(
item
.id
);
return
;
}
ItemKind
::
Mod
(
..
)
=>
self
.contains_macro_use
(
&
item
.attrs
),
_
=>
false
,
};
let
orig_current_module
=
self
.parent_scope.module
;
let
orig_current_
legacy_scope
=
self
.parent_scope.legacy
;
let
orig_current_
macro_rules_scope
=
self
.parent_scope.macro_rules
;
self
.build_reduced_graph_for_item
(
item
);
visit
::
walk_item
(
self
,
item
);
self
.parent_scope.module
=
orig_current_module
;
if
!
macro_use
{
self
.parent_scope.
legacy
=
orig_current_legacy
_scope
;
self
.parent_scope.
macro_rules
=
orig_current_macro_rules
_scope
;
}
}
fn
visit_stmt
(
&
mut
self
,
stmt
:
&
'b
ast
::
Stmt
)
{
if
let
ast
::
StmtKind
::
MacCall
(
..
)
=
stmt
.kind
{
self
.parent_scope.
legacy
=
self
.visit_invoc
(
stmt
.id
);
self
.parent_scope.
macro_rules
=
self
.visit_invoc
(
stmt
.id
);
}
else
{
visit
::
walk_stmt
(
self
,
stmt
);
}
...
...
@@ -1214,11 +1214,11 @@ fn visit_foreign_item(&mut self, foreign_item: &'b ForeignItem) {
fn
visit_block
(
&
mut
self
,
block
:
&
'b
Block
)
{
let
orig_current_module
=
self
.parent_scope.module
;
let
orig_current_
legacy_scope
=
self
.parent_scope.legacy
;
let
orig_current_
macro_rules_scope
=
self
.parent_scope.macro_rules
;
self
.build_reduced_graph_for_block
(
block
);
visit
::
walk_block
(
self
,
block
);
self
.parent_scope.module
=
orig_current_module
;
self
.parent_scope.
legacy
=
orig_current_legacy
_scope
;
self
.parent_scope.
macro_rules
=
orig_current_macro_rules
_scope
;
}
fn
visit_assoc_item
(
&
mut
self
,
item
:
&
'b
AssocItem
,
ctxt
:
AssocCtxt
)
{
...
...
src/librustc_resolve/diagnostics.rs
浏览文件 @
3fbb2549
...
...
@@ -21,7 +21,9 @@
use
crate
::
imports
::{
Import
,
ImportKind
,
ImportResolver
};
use
crate
::
path_names_to_string
;
use
crate
::{
AmbiguityError
,
AmbiguityErrorMisc
,
AmbiguityKind
};
use
crate
::{
BindingError
,
CrateLint
,
HasGenericParams
,
LegacyScope
,
Module
,
ModuleOrUniformRoot
};
use
crate
::{
BindingError
,
CrateLint
,
HasGenericParams
,
MacroRulesScope
,
Module
,
ModuleOrUniformRoot
,
};
use
crate
::{
NameBinding
,
NameBindingKind
,
PrivacyError
,
VisResolutionError
};
use
crate
::{
ParentScope
,
PathResult
,
ResolutionError
,
Resolver
,
Scope
,
ScopeSet
,
Segment
};
...
...
@@ -498,12 +500,12 @@ fn early_lookup_typo_candidate(
}
}
}
Scope
::
MacroRules
(
legacy
_scope
)
=>
{
if
let
LegacyScope
::
Binding
(
legacy_binding
)
=
legacy
_scope
{
let
res
=
legacy
_binding
.binding
.res
();
Scope
::
MacroRules
(
macro_rules
_scope
)
=>
{
if
let
MacroRulesScope
::
Binding
(
macro_rules_binding
)
=
macro_rules
_scope
{
let
res
=
macro_rules
_binding
.binding
.res
();
if
filter_fn
(
res
)
{
suggestions
.push
(
TypoSuggestion
::
from_res
(
legacy
_binding
.ident.name
,
res
))
.push
(
TypoSuggestion
::
from_res
(
macro_rules
_binding
.ident.name
,
res
))
}
}
}
...
...
src/librustc_resolve/lib.rs
浏览文件 @
3fbb2549
...
...
@@ -58,7 +58,7 @@
use
diagnostics
::{
ImportSuggestion
,
Suggestion
};
use
imports
::{
Import
,
ImportKind
,
ImportResolver
,
NameResolution
};
use
late
::{
HasGenericParams
,
PathSource
,
Rib
,
RibKind
::
*
};
use
macros
::{
LegacyBinding
,
Legacy
Scope
};
use
macros
::{
MacroRulesBinding
,
MacroRules
Scope
};
type
Res
=
def
::
Res
<
NodeId
>
;
...
...
@@ -94,7 +94,7 @@ fn determined(determined: bool) -> Determinacy {
enum
Scope
<
'a
>
{
DeriveHelpers
(
ExpnId
),
DeriveHelpersCompat
,
MacroRules
(
Legacy
Scope
<
'a
>
),
MacroRules
(
MacroRules
Scope
<
'a
>
),
CrateRoot
,
Module
(
Module
<
'a
>
),
RegisteredAttrs
,
...
...
@@ -127,7 +127,7 @@ enum ScopeSet {
pub
struct
ParentScope
<
'a
>
{
module
:
Module
<
'a
>
,
expansion
:
ExpnId
,
legacy
:
Legacy
Scope
<
'a
>
,
macro_rules
:
MacroRules
Scope
<
'a
>
,
derives
:
&
'a
[
ast
::
Path
],
}
...
...
@@ -135,7 +135,12 @@ impl<'a> ParentScope<'a> {
/// Creates a parent scope with the passed argument used as the module scope component,
/// and other scope components set to default empty values.
pub
fn
module
(
module
:
Module
<
'a
>
)
->
ParentScope
<
'a
>
{
ParentScope
{
module
,
expansion
:
ExpnId
::
root
(),
legacy
:
LegacyScope
::
Empty
,
derives
:
&
[]
}
ParentScope
{
module
,
expansion
:
ExpnId
::
root
(),
macro_rules
:
MacroRulesScope
::
Empty
,
derives
:
&
[],
}
}
}
...
...
@@ -930,9 +935,9 @@ pub struct Resolver<'a> {
/// Parent scopes in which the macros were invoked.
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
invocation_parent_scopes
:
FxHashMap
<
ExpnId
,
ParentScope
<
'a
>>
,
///
Legacy
scopes *produced* by expanding the macro invocations,
///
`macro_rules`
scopes *produced* by expanding the macro invocations,
/// include all the `macro_rules` items and other invocations generated by them.
output_
legacy_scopes
:
FxHashMap
<
ExpnId
,
Legacy
Scope
<
'a
>>
,
output_
macro_rules_scopes
:
FxHashMap
<
ExpnId
,
MacroRules
Scope
<
'a
>>
,
/// Helper attributes that are in scope for the given expansion.
helper_attrs
:
FxHashMap
<
ExpnId
,
Vec
<
Ident
>>
,
...
...
@@ -965,7 +970,7 @@ pub struct ResolverArenas<'a> {
name_bindings
:
arena
::
TypedArena
<
NameBinding
<
'a
>>
,
imports
:
arena
::
TypedArena
<
Import
<
'a
>>
,
name_resolutions
:
arena
::
TypedArena
<
RefCell
<
NameResolution
<
'a
>>>
,
legacy_bindings
:
arena
::
TypedArena
<
Legacy
Binding
<
'a
>>
,
macro_rules_bindings
:
arena
::
TypedArena
<
MacroRules
Binding
<
'a
>>
,
ast_paths
:
arena
::
TypedArena
<
ast
::
Path
>
,
}
...
...
@@ -989,8 +994,11 @@ fn alloc_import(&'a self, import: Import<'a>) -> &'a Import<'_> {
fn
alloc_name_resolution
(
&
'a
self
)
->
&
'a
RefCell
<
NameResolution
<
'a
>>
{
self
.name_resolutions
.alloc
(
Default
::
default
())
}
fn
alloc_legacy_binding
(
&
'a
self
,
binding
:
LegacyBinding
<
'a
>
)
->
&
'a
LegacyBinding
<
'a
>
{
self
.legacy_bindings
.alloc
(
binding
)
fn
alloc_macro_rules_binding
(
&
'a
self
,
binding
:
MacroRulesBinding
<
'a
>
,
)
->
&
'a
MacroRulesBinding
<
'a
>
{
self
.macro_rules_bindings
.alloc
(
binding
)
}
fn
alloc_ast_paths
(
&
'a
self
,
paths
:
&
[
ast
::
Path
])
->
&
'a
[
ast
::
Path
]
{
self
.ast_paths
.alloc_from_iter
(
paths
.iter
()
.cloned
())
...
...
@@ -1210,7 +1218,7 @@ pub fn new(
dummy_ext_derive
:
Lrc
::
new
(
SyntaxExtension
::
dummy_derive
(
session
.edition
())),
non_macro_attrs
:
[
non_macro_attr
(
false
),
non_macro_attr
(
true
)],
invocation_parent_scopes
,
output_
legacy
_scopes
:
Default
::
default
(),
output_
macro_rules
_scopes
:
Default
::
default
(),
helper_attrs
:
Default
::
default
(),
macro_defs
,
local_macro_def_scopes
:
FxHashMap
::
default
(),
...
...
@@ -1530,16 +1538,18 @@ fn visit_scopes<T>(
}
}
Scope
::
DeriveHelpers
(
..
)
=>
Scope
::
DeriveHelpersCompat
,
Scope
::
DeriveHelpersCompat
=>
Scope
::
MacroRules
(
parent_scope
.legacy
),
Scope
::
MacroRules
(
legacy_scope
)
=>
match
legacy_scope
{
LegacyScope
::
Binding
(
binding
)
=>
Scope
::
MacroRules
(
binding
.parent_legacy_scope
),
LegacyScope
::
Invocation
(
invoc_id
)
=>
Scope
::
MacroRules
(
self
.output_legacy_scopes
Scope
::
DeriveHelpersCompat
=>
Scope
::
MacroRules
(
parent_scope
.macro_rules
),
Scope
::
MacroRules
(
macro_rules_scope
)
=>
match
macro_rules_scope
{
MacroRulesScope
::
Binding
(
binding
)
=>
{
Scope
::
MacroRules
(
binding
.parent_macro_rules_scope
)
}
MacroRulesScope
::
Invocation
(
invoc_id
)
=>
Scope
::
MacroRules
(
self
.output_macro_rules_scopes
.get
(
&
invoc_id
)
.cloned
()
.unwrap_or
(
self
.invocation_parent_scopes
[
&
invoc_id
]
.
legacy
),
.unwrap_or
(
self
.invocation_parent_scopes
[
&
invoc_id
]
.
macro_rules
),
),
Legacy
Scope
::
Empty
=>
Scope
::
Module
(
module
),
MacroRules
Scope
::
Empty
=>
Scope
::
Module
(
module
),
},
Scope
::
CrateRoot
=>
match
ns
{
TypeNS
=>
{
...
...
src/librustc_resolve/macros.rs
浏览文件 @
3fbb2549
...
...
@@ -33,26 +33,26 @@
type
Res
=
def
::
Res
<
NodeId
>
;
/// Binding produced by a `macro_rules` item.
/// Not modularized, can shadow previous
legacy
bindings, etc.
/// Not modularized, can shadow previous
`macro_rules`
bindings, etc.
#[derive(Debug)]
pub
struct
Legacy
Binding
<
'a
>
{
pub
struct
MacroRules
Binding
<
'a
>
{
crate
binding
:
&
'a
NameBinding
<
'a
>
,
///
Legacy
scope into which the `macro_rules` item was planted.
crate
parent_
legacy_scope
:
Legacy
Scope
<
'a
>
,
///
`macro_rules`
scope into which the `macro_rules` item was planted.
crate
parent_
macro_rules_scope
:
MacroRules
Scope
<
'a
>
,
crate
ident
:
Ident
,
}
/// The scope introduced by a `macro_rules!` macro.
/// This starts at the macro's definition and ends at the end of the macro's parent
/// module (named or unnamed), or even further if it escapes with `#[macro_use]`.
/// Some macro invocations need to introduce
legacy
scopes too because they
/// Some macro invocations need to introduce
`macro_rules`
scopes too because they
/// can potentially expand into macro definitions.
#[derive(Copy,
Clone,
Debug)]
pub
enum
Legacy
Scope
<
'a
>
{
pub
enum
MacroRules
Scope
<
'a
>
{
/// Empty "root" scope at the crate start containing no names.
Empty
,
/// The scope introduced by a `macro_rules!` macro definition.
Binding
(
&
'a
Legacy
Binding
<
'a
>
),
Binding
(
&
'a
MacroRules
Binding
<
'a
>
),
/// The scope introduced by a macro invocation that can potentially
/// create a `macro_rules!` macro definition.
Invocation
(
ExpnId
),
...
...
@@ -159,8 +159,8 @@ fn visit_ast_fragment_with_placeholders(&mut self, expansion: ExpnId, fragment:
// Integrate the new AST fragment into all the definition and module structures.
// We are inside the `expansion` now, but other parent scope components are still the same.
let
parent_scope
=
ParentScope
{
expansion
,
..
self
.invocation_parent_scopes
[
&
expansion
]
};
let
output_
legacy
_scope
=
self
.build_reduced_graph
(
fragment
,
parent_scope
);
self
.output_
legacy_scopes
.insert
(
expansion
,
output_legacy
_scope
);
let
output_
macro_rules
_scope
=
self
.build_reduced_graph
(
fragment
,
parent_scope
);
self
.output_
macro_rules_scopes
.insert
(
expansion
,
output_macro_rules
_scope
);
parent_scope
.module.unexpanded_invocations
.borrow_mut
()
.remove
(
&
expansion
);
}
...
...
@@ -608,12 +608,14 @@ struct Flags: u8 {
}
result
}
Scope
::
MacroRules
(
legacy_scope
)
=>
match
legacy_scope
{
LegacyScope
::
Binding
(
legacy_binding
)
if
ident
==
legacy_binding
.ident
=>
{
Ok
((
legacy_binding
.binding
,
Flags
::
MACRO_RULES
))
Scope
::
MacroRules
(
macro_rules_scope
)
=>
match
macro_rules_scope
{
MacroRulesScope
::
Binding
(
macro_rules_binding
)
if
ident
==
macro_rules_binding
.ident
=>
{
Ok
((
macro_rules_binding
.binding
,
Flags
::
MACRO_RULES
))
}
Legacy
Scope
::
Invocation
(
invoc_id
)
if
!
this
.output_
legacy
_scopes
.contains_key
(
&
invoc_id
)
=>
MacroRules
Scope
::
Invocation
(
invoc_id
)
if
!
this
.output_
macro_rules
_scopes
.contains_key
(
&
invoc_id
)
=>
{
Err
(
Determinacy
::
Undetermined
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录