Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d052d28d
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,发现更多精彩内容 >>
提交
d052d28d
编写于
11月 29, 2017
作者:
J
Jeffrey Seyfried
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve interaction between macros 2.0 and `macro_rules!`.
上级
3dfbc88a
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
126 addition
and
9 deletion
+126
-9
src/libproc_macro/lib.rs
src/libproc_macro/lib.rs
+1
-1
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/build_reduced_graph.rs
+1
-1
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+14
-5
src/librustc_resolve/macros.rs
src/librustc_resolve/macros.rs
+1
-1
src/librustc_resolve/resolve_imports.rs
src/librustc_resolve/resolve_imports.rs
+1
-1
src/libsyntax_pos/hygiene.rs
src/libsyntax_pos/hygiene.rs
+39
-0
src/test/run-pass/hygiene/auxiliary/legacy_interaction.rs
src/test/run-pass/hygiene/auxiliary/legacy_interaction.rs
+19
-0
src/test/run-pass/hygiene/legacy_interaction.rs
src/test/run-pass/hygiene/legacy_interaction.rs
+50
-0
未找到文件。
src/libproc_macro/lib.rs
浏览文件 @
d052d28d
...
@@ -95,7 +95,7 @@ fn from_str(src: &str) -> Result<TokenStream, LexError> {
...
@@ -95,7 +95,7 @@ fn from_str(src: &str) -> Result<TokenStream, LexError> {
// notify the expansion info that it is unhygienic
// notify the expansion info that it is unhygienic
let
mark
=
Mark
::
fresh
(
mark
);
let
mark
=
Mark
::
fresh
(
mark
);
mark
.set_expn_info
(
expn_info
);
mark
.set_expn_info
(
expn_info
);
let
span
=
call_site
.with_ctxt
(
call_site
.ctxt
()
.apply_mark
(
mark
));
let
span
=
call_site
.with_ctxt
(
SyntaxContext
::
empty
()
.apply_mark
(
mark
));
let
stream
=
parse
::
parse_stream_from_source_str
(
name
,
src
,
sess
,
Some
(
span
));
let
stream
=
parse
::
parse_stream_from_source_str
(
name
,
src
,
sess
,
Some
(
span
));
Ok
(
__
internal
::
token_stream_wrap
(
stream
))
Ok
(
__
internal
::
token_stream_wrap
(
stream
))
})
})
...
...
src/librustc_resolve/build_reduced_graph.rs
浏览文件 @
d052d28d
...
@@ -156,7 +156,7 @@ fn build_reduced_graph_for_use_tree(&mut self,
...
@@ -156,7 +156,7 @@ fn build_reduced_graph_for_use_tree(&mut self,
// 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
.ctxt
);
let
crate_root
=
self
.resolve_crate_root
(
source
.ctxt
,
true
);
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!
(),
...
...
src/librustc_resolve/lib.rs
浏览文件 @
d052d28d
...
@@ -42,7 +42,7 @@
...
@@ -42,7 +42,7 @@
use
rustc
::
util
::
nodemap
::{
NodeMap
,
NodeSet
,
FxHashMap
,
FxHashSet
,
DefIdMap
};
use
rustc
::
util
::
nodemap
::{
NodeMap
,
NodeSet
,
FxHashMap
,
FxHashSet
,
DefIdMap
};
use
syntax
::
codemap
::{
dummy_spanned
,
respan
};
use
syntax
::
codemap
::{
dummy_spanned
,
respan
};
use
syntax
::
ext
::
hygiene
::{
Mark
,
SyntaxContext
};
use
syntax
::
ext
::
hygiene
::{
Mark
,
MarkKind
,
SyntaxContext
};
use
syntax
::
ast
::{
self
,
Name
,
NodeId
,
Ident
,
SpannedIdent
,
FloatTy
,
IntTy
,
UintTy
};
use
syntax
::
ast
::{
self
,
Name
,
NodeId
,
Ident
,
SpannedIdent
,
FloatTy
,
IntTy
,
UintTy
};
use
syntax
::
ext
::
base
::
SyntaxExtension
;
use
syntax
::
ext
::
base
::
SyntaxExtension
;
use
syntax
::
ext
::
base
::
Determinacy
::{
self
,
Determined
,
Undetermined
};
use
syntax
::
ext
::
base
::
Determinacy
::{
self
,
Determined
,
Undetermined
};
...
@@ -1775,8 +1775,17 @@ fn resolve_ident_in_module(&mut self,
...
@@ -1775,8 +1775,17 @@ fn resolve_ident_in_module(&mut self,
result
result
}
}
fn
resolve_crate_root
(
&
mut
self
,
mut
ctxt
:
SyntaxContext
)
->
Module
<
'a
>
{
fn
resolve_crate_root
(
&
mut
self
,
mut
ctxt
:
SyntaxContext
,
legacy
:
bool
)
->
Module
<
'a
>
{
let
module
=
match
ctxt
.adjust
(
Mark
::
root
())
{
let
mark
=
if
legacy
{
// 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`
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
ctxt
.marks
()
.into_iter
()
.find
(|
&
mark
|
mark
.kind
()
!=
MarkKind
::
Modern
)
}
else
{
ctxt
=
ctxt
.modern
();
ctxt
.adjust
(
Mark
::
root
())
};
let
module
=
match
mark
{
Some
(
def
)
=>
self
.macro_def_scope
(
def
),
Some
(
def
)
=>
self
.macro_def_scope
(
def
),
None
=>
return
self
.graph_root
,
None
=>
return
self
.graph_root
,
};
};
...
@@ -2961,11 +2970,11 @@ fn resolve_path(&mut self,
...
@@ -2961,11 +2970,11 @@ fn resolve_path(&mut self,
(
i
==
1
&&
name
==
keywords
::
Crate
.name
()
&&
(
i
==
1
&&
name
==
keywords
::
Crate
.name
()
&&
path
[
0
]
.node.name
==
keywords
::
CrateRoot
.name
())
{
path
[
0
]
.node.name
==
keywords
::
CrateRoot
.name
())
{
// `::a::b` or `::crate::a::b`
// `::a::b` or `::crate::a::b`
module
=
Some
(
self
.resolve_crate_root
(
ident
.node.ctxt
.modern
()
));
module
=
Some
(
self
.resolve_crate_root
(
ident
.node.ctxt
,
false
));
continue
continue
}
else
if
i
==
0
&&
name
==
keywords
::
DollarCrate
.name
()
{
}
else
if
i
==
0
&&
name
==
keywords
::
DollarCrate
.name
()
{
// `$crate::a::b`
// `$crate::a::b`
module
=
Some
(
self
.resolve_crate_root
(
ident
.node.ctxt
));
module
=
Some
(
self
.resolve_crate_root
(
ident
.node.ctxt
,
true
));
continue
continue
}
else
if
i
==
1
&&
self
.session.features
.borrow
()
.extern_absolute_paths
&&
}
else
if
i
==
1
&&
self
.session.features
.borrow
()
.extern_absolute_paths
&&
path
[
0
]
.node.name
==
keywords
::
CrateRoot
.name
()
&&
path
[
0
]
.node.name
==
keywords
::
CrateRoot
.name
()
&&
...
...
src/librustc_resolve/macros.rs
浏览文件 @
d052d28d
...
@@ -140,7 +140,7 @@ fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
...
@@ -140,7 +140,7 @@ fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
let
ident
=
path
.segments
[
0
]
.identifier
;
let
ident
=
path
.segments
[
0
]
.identifier
;
if
ident
.name
==
keywords
::
DollarCrate
.name
()
{
if
ident
.name
==
keywords
::
DollarCrate
.name
()
{
path
.segments
[
0
]
.identifier.name
=
keywords
::
CrateRoot
.name
();
path
.segments
[
0
]
.identifier.name
=
keywords
::
CrateRoot
.name
();
let
module
=
self
.0
.resolve_crate_root
(
ident
.ctxt
);
let
module
=
self
.0
.resolve_crate_root
(
ident
.ctxt
,
true
);
if
!
module
.is_local
()
{
if
!
module
.is_local
()
{
let
span
=
path
.segments
[
0
]
.span
;
let
span
=
path
.segments
[
0
]
.span
;
path
.segments
.insert
(
1
,
match
module
.kind
{
path
.segments
.insert
(
1
,
match
module
.kind
{
...
...
src/librustc_resolve/resolve_imports.rs
浏览文件 @
d052d28d
...
@@ -620,7 +620,7 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Spa
...
@@ -620,7 +620,7 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Spa
"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
.ctxt
.modern
()))
Some
(
self
.resolve_crate_root
(
source
.ctxt
.modern
()
,
false
))
}
}
}
else
if
extern_absolute_paths
&&
}
else
if
extern_absolute_paths
&&
!
token
::
Ident
(
source
)
.is_path_segment_keyword
()
{
!
token
::
Ident
(
source
)
.is_path_segment_keyword
()
{
...
...
src/libsyntax_pos/hygiene.rs
浏览文件 @
d052d28d
...
@@ -181,6 +181,33 @@ pub fn allocate_directly(expansion_info: ExpnInfo) -> Self {
...
@@ -181,6 +181,33 @@ pub fn allocate_directly(expansion_info: ExpnInfo) -> Self {
/// Extend a syntax context with a given mark
/// Extend a syntax context with a given mark
pub
fn
apply_mark
(
self
,
mark
:
Mark
)
->
SyntaxContext
{
pub
fn
apply_mark
(
self
,
mark
:
Mark
)
->
SyntaxContext
{
if
mark
.kind
()
==
MarkKind
::
Modern
{
return
self
.apply_mark_internal
(
mark
);
}
let
call_site_ctxt
=
mark
.expn_info
()
.map_or
(
SyntaxContext
::
empty
(),
|
info
|
info
.call_site
.ctxt
())
.modern
();
if
call_site_ctxt
==
SyntaxContext
::
empty
()
{
return
self
.apply_mark_internal
(
mark
);
}
// Otherwise, `mark` is a macros 1.0 definition and the call site is in a
// macros 2.0 expansion, i.e. a macros 1.0 invocation is in a macros 2.0 definition.
//
// In this case, the tokens from the macros 1.0 definition inherit the hygiene
// at their invocation. That is, we pretend that the macros 1.0 definition
// was defined at its invocation (i.e. inside the macros 2.0 definition)
// so that the macros 2.0 definition remains hygienic.
//
// See the example at `test/run-pass/hygiene/legacy_interaction.rs`.
let
mut
ctxt
=
call_site_ctxt
;
for
mark
in
self
.marks
()
{
ctxt
=
ctxt
.apply_mark_internal
(
mark
);
}
ctxt
.apply_mark_internal
(
mark
)
}
fn
apply_mark_internal
(
self
,
mark
:
Mark
)
->
SyntaxContext
{
HygieneData
::
with
(|
data
|
{
HygieneData
::
with
(|
data
|
{
let
syntax_contexts
=
&
mut
data
.syntax_contexts
;
let
syntax_contexts
=
&
mut
data
.syntax_contexts
;
let
mut
modern
=
syntax_contexts
[
self
.0
as
usize
]
.modern
;
let
mut
modern
=
syntax_contexts
[
self
.0
as
usize
]
.modern
;
...
@@ -215,6 +242,18 @@ pub fn remove_mark(&mut self) -> Mark {
...
@@ -215,6 +242,18 @@ pub fn remove_mark(&mut self) -> Mark {
})
})
}
}
pub
fn
marks
(
mut
self
)
->
Vec
<
Mark
>
{
HygieneData
::
with
(|
data
|
{
let
mut
marks
=
Vec
::
new
();
while
self
!=
SyntaxContext
::
empty
()
{
marks
.push
(
data
.syntax_contexts
[
self
.0
as
usize
]
.outer_mark
);
self
=
data
.syntax_contexts
[
self
.0
as
usize
]
.prev_ctxt
;
}
marks
.reverse
();
marks
})
}
/// Adjust this context for resolution in a scope created by the given expansion.
/// Adjust this context for resolution in a scope created by the given expansion.
/// For example, consider the following three resolutions of `f`:
/// For example, consider the following three resolutions of `f`:
/// ```rust
/// ```rust
...
...
src/test/run-pass/hygiene/auxiliary/legacy_interaction.rs
0 → 100644
浏览文件 @
d052d28d
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-pretty pretty-printing is unhygienic
#[macro_export]
macro_rules!
m
{
()
=>
{
fn
f
()
{}
// (2)
g
();
// (1)
}
}
src/test/run-pass/hygiene/legacy_interaction.rs
0 → 100644
浏览文件 @
d052d28d
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-pretty pretty-printing is unhygienic
// aux-build:legacy_interaction.rs
#![feature(decl_macro)]
#[allow(unused)]
extern
crate
legacy_interaction
;
// ^ defines
// ```rust
// macro_rules! m {
// () => {
// fn f() // (1)
// g() // (2)
// }
// }
// ```rust
mod
def_site
{
// Unless this macro opts out of hygiene, it should resolve the same wherever it is invoked.
pub
macro
m2
()
{
::
legacy_interaction
::
m!
();
f
();
// This should resolve to (1)
fn
g
()
{}
// We want (2) resolve to this, not to (4)
}
}
mod
use_site
{
fn
test
()
{
fn
f
()
->
bool
{
true
}
// (3)
fn
g
()
->
bool
{
true
}
// (4)
::
def_site
::
m2!
();
let
_
:
bool
=
f
();
// This should resolve to (3)
let
_
:
bool
=
g
();
// This should resolve to (4)
}
}
fn
main
()
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录