Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
fe60f19f
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,发现更多精彩内容 >>
未验证
提交
fe60f19f
编写于
3月 01, 2021
作者:
A
Aaron Hill
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ban custom inner attributes in expressions and statements
上级
cb473c2c
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
240 addition
and
156 deletion
+240
-156
compiler/rustc_expand/src/expand.rs
compiler/rustc_expand/src/expand.rs
+13
-7
compiler/rustc_resolve/src/macros.rs
compiler/rustc_resolve/src/macros.rs
+15
-6
src/test/ui/proc-macro/inner-attrs.rs
src/test/ui/proc-macro/inner-attrs.rs
+19
-2
src/test/ui/proc-macro/inner-attrs.stderr
src/test/ui/proc-macro/inner-attrs.stderr
+32
-0
src/test/ui/proc-macro/inner-attrs.stdout
src/test/ui/proc-macro/inner-attrs.stdout
+161
-141
未找到文件。
compiler/rustc_expand/src/expand.rs
浏览文件 @
fe60f19f
...
...
@@ -206,30 +206,36 @@ impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
}
}
pub
enum
SupportsMacroExpansion
{
No
,
Yes
{
supports_inner_attrs
:
bool
},
}
impl
AstFragmentKind
{
crate
fn
dummy
(
self
,
span
:
Span
)
->
AstFragment
{
self
.make_from
(
DummyResult
::
any
(
span
))
.expect
(
"couldn't create a dummy AST fragment"
)
}
/// Fragment supports macro expansion and not just inert attributes, `cfg` and `cfg_attr`.
pub
fn
supports_macro_expansion
(
self
)
->
bool
{
pub
fn
supports_macro_expansion
(
self
)
->
SupportsMacroExpansion
{
match
self
{
AstFragmentKind
::
OptExpr
|
AstFragmentKind
::
Expr
|
AstFragmentKind
::
Pat
|
AstFragmentKind
::
Ty
|
AstFragmentKind
::
Stmts
|
AstFragmentKind
::
Items
|
AstFragmentKind
::
Ty
|
AstFragmentKind
::
Pat
=>
SupportsMacroExpansion
::
Yes
{
supports_inner_attrs
:
false
},
AstFragmentKind
::
Items
|
AstFragmentKind
::
TraitItems
|
AstFragmentKind
::
ImplItems
|
AstFragmentKind
::
ForeignItems
=>
true
,
|
AstFragmentKind
::
ForeignItems
=>
{
SupportsMacroExpansion
::
Yes
{
supports_inner_attrs
:
true
}
}
AstFragmentKind
::
Arms
|
AstFragmentKind
::
Fields
|
AstFragmentKind
::
FieldPats
|
AstFragmentKind
::
GenericParams
|
AstFragmentKind
::
Params
|
AstFragmentKind
::
StructFields
|
AstFragmentKind
::
Variants
=>
false
,
|
AstFragmentKind
::
Variants
=>
SupportsMacroExpansion
::
No
,
}
}
...
...
compiler/rustc_resolve/src/macros.rs
浏览文件 @
fe60f19f
...
...
@@ -17,7 +17,7 @@
use
rustc_expand
::
base
::
Annotatable
;
use
rustc_expand
::
base
::{
Indeterminate
,
ResolverExpand
,
SyntaxExtension
,
SyntaxExtensionKind
};
use
rustc_expand
::
compile_declarative_macro
;
use
rustc_expand
::
expand
::{
AstFragment
,
Invocation
,
InvocationKind
};
use
rustc_expand
::
expand
::{
AstFragment
,
Invocation
,
InvocationKind
,
SupportsMacroExpansion
};
use
rustc_feature
::
is_builtin_attr_name
;
use
rustc_hir
::
def
::{
self
,
DefKind
,
NonMacroAttrKind
};
use
rustc_hir
::
def_id
;
...
...
@@ -278,12 +278,12 @@ fn resolve_macro_invocation(
// Derives are not included when `invocations` are collected, so we have to add them here.
let
parent_scope
=
&
ParentScope
{
derives
,
..
parent_scope
};
let
require_inert
=
!
invoc
.fragment_kind
.supports_macro_expansion
();
let
supports_macro_expansion
=
invoc
.fragment_kind
.supports_macro_expansion
();
let
node_id
=
self
.lint_node_id
(
eager_expansion_root
);
let
(
ext
,
res
)
=
self
.smart_resolve_macro_path
(
path
,
kind
,
require_inert
,
supports_macro_expansion
,
inner_attr
,
parent_scope
,
node_id
,
...
...
@@ -457,7 +457,7 @@ fn smart_resolve_macro_path(
&
mut
self
,
path
:
&
ast
::
Path
,
kind
:
MacroKind
,
require_inert
:
bool
,
supports_macro_expansion
:
SupportsMacroExpansion
,
inner_attr
:
bool
,
parent_scope
:
&
ParentScope
<
'a
>
,
node_id
:
NodeId
,
...
...
@@ -505,8 +505,17 @@ fn smart_resolve_macro_path(
let
unexpected_res
=
if
ext
.macro_kind
()
!=
kind
{
Some
((
kind
.article
(),
kind
.descr_expected
()))
}
else
if
require_inert
&&
matches!
(
res
,
Res
::
Def
(
..
))
{
Some
((
"a"
,
"non-macro attribute"
))
}
else
if
matches!
(
res
,
Res
::
Def
(
..
))
{
match
supports_macro_expansion
{
SupportsMacroExpansion
::
No
=>
Some
((
"a"
,
"non-macro attribute"
)),
SupportsMacroExpansion
::
Yes
{
supports_inner_attrs
}
=>
{
if
inner_attr
&&
!
supports_inner_attrs
{
Some
((
"a"
,
"non-macro inner attribute"
))
}
else
{
None
}
}
}
}
else
{
None
};
...
...
src/test/ui/proc-macro/inner-attrs.rs
浏览文件 @
fe60f19f
// check-pass
// compile-flags: -Z span-debug --error-format human
// aux-build:test-macros.rs
#![feature(custom_inner_attributes)]
#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]
#![feature(rustc_attrs)]
#![no_std]
// Don't load unnecessary hygiene information from std
extern
crate
std
;
...
...
@@ -25,17 +25,34 @@ struct MyStruct {
fn
bar
()
{
(
#![print_target_and_args(fifth)]
1
,
2
);
//~^ ERROR expected non-macro inner attribute, found attribute macro
#[print_target_and_args(tuple_attrs)]
(
#![cfg_attr(FALSE,
rustc_dummy)]
3
,
4
,
{
#![cfg_attr(not(FALSE),
rustc_dummy(innermost))]
5
}
);
#[print_target_and_args(array_attrs)]
[
#![rustc_dummy(inner)]
true
;
0
];
[
#![print_target_and_args(sixth)]
1
,
2
];
//~^ ERROR expected non-macro inner attribute, found attribute macro
[
#![print_target_and_args(seventh)]
true
;
5
];
//~^ ERROR expected non-macro inner attribute, found attribute macro
match
0
{
#![print_target_and_args(eighth)]
//~^ ERROR expected non-macro inner attribute, found attribute macro
_
=>
{}
}
MyStruct
{
#![print_target_and_args(ninth)]
field
:
true
};
//~^ ERROR expected non-macro inner attribute, found attribute macro
}
extern
{
...
...
src/test/ui/proc-macro/inner-attrs.stderr
0 → 100644
浏览文件 @
fe60f19f
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
--> $DIR/inner-attrs.rs:27:9
|
LL | (#![print_target_and_args(fifth)] 1, 2);
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
--> $DIR/inner-attrs.rs:43:9
|
LL | [#![print_target_and_args(sixth)] 1 , 2];
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
--> $DIR/inner-attrs.rs:45:9
|
LL | [#![print_target_and_args(seventh)] true ; 5];
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
--> $DIR/inner-attrs.rs:49:12
|
LL | #![print_target_and_args(eighth)]
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
--> $DIR/inner-attrs.rs:54:19
|
LL | MyStruct { #![print_target_and_args(ninth)] field: true };
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
error: aborting due to 5 previous errors
src/test/ui/proc-macro/inner-attrs.stdout
浏览文件 @
fe60f19f
...
...
@@ -290,231 +290,251 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/inner-attrs.rs:17:1: 20:2 (#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY):
fifth
PRINT-ATTR_ARGS INPUT (DISPLAY):
tuple_attrs
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
Ident {
ident: "
fifth
",
span: $DIR/inner-attrs.rs:
27:31: 27:36
(#0),
ident: "
tuple_attrs
",
span: $DIR/inner-attrs.rs:
30:29: 30:40
(#0),
},
]
PRINT-ATTR INPUT (DISPLAY): (1, 2) ;
PRINT-ATTR INPUT (DISPLAY): (# ! [cfg_attr(FALSE, rustc_dummy)] 3, 4,
{ # ! [cfg_attr(not(FALSE), rustc_dummy(innermost))] 5 }) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
kind: Integer,
symbol: "1",
suffix: None,
span: $DIR/inner-attrs.rs:27:5: 27:45 (#0),
Punct {
ch: '#',
spacing: Joint,
span: $DIR/inner-attrs.rs:31:9: 31:10 (#0),
},
Punct {
ch: '
,
',
ch: '
!
',
spacing: Alone,
span: $DIR/inner-attrs.rs:
27:5: 27:45
(#0),
span: $DIR/inner-attrs.rs:
31:10: 31:11
(#0),
},
Literal {
kind: Integer,
symbol: "2",
suffix: None,
span: $DIR/inner-attrs.rs:27:5: 27:45 (#0),
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "cfg_attr",
span: $DIR/inner-attrs.rs:31:12: 31:20 (#0),
},
],
span: $DIR/inner-attrs.rs:27:5: 27:45 (#0),
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "FALSE",
span: $DIR/inner-attrs.rs:31:21: 31:26 (#0),
},
Punct {
ch: ';
',
ch: ',
',
spacing: Alone,
span: $DIR/inner-attrs.rs:27:5: 27:45
(#0),
span: $DIR/inner-attrs.rs:31:26: 31:27
(#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY): sixth
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
Ident {
ident: "sixth",
span: $DIR/inner-attrs.rs:29:31: 29:36 (#0),
ident: "rustc_dummy",
span: $DIR/inner-attrs.rs:31:28: 31:39 (#0),
},
],
span: $DIR/inner-attrs.rs:31:20: 31:40 (#0),
},
],
span: $DIR/inner-attrs.rs:31:11: 31:41 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): [1, 2] ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Group {
delimiter: Bracket,
stream: TokenStream [
Literal {
kind: Integer,
symbol: "
1
",
symbol: "
3
",
suffix: None,
span: $DIR/inner-attrs.rs:
29:5: 29:46
(#0),
span: $DIR/inner-attrs.rs:
32:9: 32:10
(#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/inner-attrs.rs:
29:5: 29:46
(#0),
span: $DIR/inner-attrs.rs:
32:10: 32:11
(#0),
},
Literal {
kind: Integer,
symbol: "
2
",
symbol: "
4
",
suffix: None,
span: $DIR/inner-attrs.rs:
29:5: 29:46
(#0),
span: $DIR/inner-attrs.rs:
32:12: 32:13
(#0),
},
],
span: $DIR/inner-attrs.rs:29:5: 29:46 (#0),
Punct {
ch: ',',
spacing: Alone,
span: $DIR/inner-attrs.rs:32:13: 32:14 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: ';',
ch: '#',
spacing: Joint,
span: $DIR/inner-attrs.rs:33:13: 33:14 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/inner-attrs.rs:29:5: 29:46
(#0),
span: $DIR/inner-attrs.rs:33:14: 33:15
(#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY): seventh
PRINT-ATTR_ARGS INPUT (DEBUG)
: TokenStream [
Group {
delimiter: Bracket,
stream
: TokenStream [
Ident {
ident: "seventh
",
span: $DIR/inner-attrs.rs:30:31: 30:38
(#0),
ident: "cfg_attr
",
span: $DIR/inner-attrs.rs:33:16: 33:24
(#0),
},
]
PRINT-ATTR INPUT (DISPLAY): [true ; 5] ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Group {
delimiter: Bracket
,
delimiter: Parenthesis
,
stream: TokenStream [
Ident {
ident: "true",
span: $DIR/inner-attrs.rs:30:5: 30:51 (#0),
ident: "not",
span: $DIR/inner-attrs.rs:33:25: 33:28 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "FALSE",
span: $DIR/inner-attrs.rs:33:29: 33:34 (#0),
},
],
span: $DIR/inner-attrs.rs:33:28: 33:35 (#0),
},
Punct {
ch: ';
',
ch: ',
',
spacing: Alone,
span: $DIR/inner-attrs.rs:30:5: 30:51 (#0),
span: $DIR/inner-attrs.rs:33:35: 33:36 (#0),
},
Ident {
ident: "rustc_dummy",
span: $DIR/inner-attrs.rs:33:37: 33:48 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "innermost",
span: $DIR/inner-attrs.rs:33:49: 33:58 (#0),
},
],
span: $DIR/inner-attrs.rs:33:48: 33:59 (#0),
},
],
span: $DIR/inner-attrs.rs:33:24: 33:60 (#0),
},
],
span: $DIR/inner-attrs.rs:33:15: 33:61 (#0),
},
Literal {
kind: Integer,
symbol: "5",
suffix: None,
span: $DIR/inner-attrs.rs:30:5: 30:51 (#0),
span: $DIR/inner-attrs.rs:34:13: 34:14 (#0),
},
],
span: $DIR/inner-attrs.rs:32:15: 35:10 (#0),
},
],
span: $DIR/inner-attrs.rs:30:
5: 30:51
(#0),
span: $DIR/inner-attrs.rs:30:
43: 36:6
(#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/inner-attrs.rs:3
0:5: 30:51
(#0),
span: $DIR/inner-attrs.rs:3
6:6: 36:7
(#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY):
eighth
PRINT-ATTR_ARGS INPUT (DISPLAY):
array_attrs
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
Ident {
ident: "
eighth
",
span: $DIR/inner-attrs.rs:3
4:34: 34
:40 (#0),
ident: "
array_attrs
",
span: $DIR/inner-attrs.rs:3
8:29: 38
:40 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY):
match 0 { _ => { } }
PRINT-ATTR INPUT (DISPLAY):
[# ! [rustc_dummy(inner)] true ; 0] ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "match",
span: $DIR/inner-attrs.rs:33:5: 36:6 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/inner-attrs.rs:33:5: 36:6 (#0),
},
Group {
delimiter: Brac
e
,
delimiter: Brac
ket
,
stream: TokenStream [
Ident {
ident: "_",
span: $DIR/inner-attrs.rs:33:5: 36:6 (#0),
},
Punct {
ch: '
=
',
ch: '
#
',
spacing: Joint,
span: $DIR/inner-attrs.rs:3
3:5: 36:6
(#0),
span: $DIR/inner-attrs.rs:3
8:43: 41:7
(#0),
},
Punct {
ch: '
>
',
ch: '
!
',
spacing: Alone,
span: $DIR/inner-attrs.rs:3
3:5: 36:6
(#0),
span: $DIR/inner-attrs.rs:3
8:43: 41:7
(#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/inner-attrs.rs:33:5: 36:6 (#0),
},
],
span: $DIR/inner-attrs.rs:33:5: 36:6 (#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY): ninth
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
Ident {
ident: "ninth",
span: $DIR/inner-attrs.rs:38:41: 38:46 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): MyStruct { field : true, } ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "MyStruct
",
span: $DIR/inner-attrs.rs:38:5: 38:63
(#0),
ident: "rustc_dummy
",
span: $DIR/inner-attrs.rs:38:43: 41:7
(#0),
},
Group {
delimiter: Brace
,
delimiter: Parenthesis
,
stream: TokenStream [
Ident {
ident: "field
",
span: $DIR/inner-attrs.rs:38:5: 38:63
(#0),
ident: "inner
",
span: $DIR/inner-attrs.rs:38:43: 41:7
(#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/inner-attrs.rs:38:5: 38:63 (#0),
],
span: $DIR/inner-attrs.rs:38:43: 41:7 (#0),
},
],
span: $DIR/inner-attrs.rs:38:43: 41:7 (#0),
},
Ident {
ident: "true",
span: $DIR/inner-attrs.rs:38:
5: 38:63
(#0),
span: $DIR/inner-attrs.rs:38:
43: 41:7
(#0),
},
Punct {
ch: '
,
',
ch: '
;
',
spacing: Alone,
span: $DIR/inner-attrs.rs:38:5: 38:63 (#0),
span: $DIR/inner-attrs.rs:38:43: 41:7 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/inner-attrs.rs:38:43: 41:7 (#0),
},
],
span: $DIR/inner-attrs.rs:38:
5: 38:63
(#0),
span: $DIR/inner-attrs.rs:38:
43: 41:7
(#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/inner-attrs.rs:38:
5: 38:63
(#0),
span: $DIR/inner-attrs.rs:38:
43: 41:7
(#0),
},
]
PRINT-ATTR_ARGS INPUT (DISPLAY): tenth
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
Ident {
ident: "tenth",
span: $DIR/inner-attrs.rs:
43:42: 43
:47 (#0),
span: $DIR/inner-attrs.rs:
60:42: 60
:47 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): fn weird_extern() { }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "fn",
span: $DIR/inner-attrs.rs:
42:5: 44
:6 (#0),
span: $DIR/inner-attrs.rs:
59:5: 61
:6 (#0),
},
Ident {
ident: "weird_extern",
span: $DIR/inner-attrs.rs:
42:5: 44
:6 (#0),
span: $DIR/inner-attrs.rs:
59:5: 61
:6 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/inner-attrs.rs:
42:5: 44
:6 (#0),
span: $DIR/inner-attrs.rs:
59:5: 61
:6 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/inner-attrs.rs:
42:5: 44
:6 (#0),
span: $DIR/inner-attrs.rs:
59:5: 61
:6 (#0),
},
]
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录