Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
1db4d607
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,发现更多精彩内容 >>
提交
1db4d607
编写于
10月 28, 2019
作者:
M
Mazdak Farrokhzad
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parser: allow ABIs from literal macro fragments
上级
49def076
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
63 addition
and
26 deletion
+63
-26
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+27
-13
src/libsyntax/parse/parser/expr.rs
src/libsyntax/parse/parser/expr.rs
+5
-1
src/libsyntax/parse/parser/item.rs
src/libsyntax/parse/parser/item.rs
+1
-1
src/libsyntax/parse/token.rs
src/libsyntax/parse/token.rs
+0
-7
src/test/ui/parser/bad-lit-suffixes.rs
src/test/ui/parser/bad-lit-suffixes.rs
+2
-2
src/test/ui/parser/bad-lit-suffixes.stderr
src/test/ui/parser/bad-lit-suffixes.stderr
+2
-2
src/test/ui/parser/extern-abi-from-mac-literal-frag.rs
src/test/ui/parser/extern-abi-from-mac-literal-frag.rs
+26
-0
未找到文件。
src/libsyntax/parse/parser.rs
浏览文件 @
1db4d607
...
...
@@ -1205,27 +1205,41 @@ fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
Ok
(())
}
/// Parses `extern` followed by an optional ABI string, or nothing.
/// Parses `extern string_literal?`.
/// If `extern` is not found, the Rust ABI is used.
/// If `extern` is found and a `string_literal` does not follow, the C ABI is used.
fn
parse_extern_abi
(
&
mut
self
)
->
PResult
<
'a
,
Abi
>
{
Ok
(
if
self
.eat_keyword
(
kw
::
Extern
)
{
let
ext_sp
=
self
.prev_span
;
self
.parse_opt_abi
()
?
.unwrap_or_else
(||
Abi
::
new
(
sym
::
C
,
ext_sp
))
self
.parse_opt_abi
()
?
}
else
{
Abi
::
default
()
})
}
/// Parses a string as an ABI spec on an extern type or module.
fn
parse_opt_abi
(
&
mut
self
)
->
PResult
<
'a
,
Option
<
Abi
>>
{
match
self
.token.kind
{
token
::
Literal
(
token
::
Lit
{
kind
:
token
::
Str
,
symbol
,
suffix
})
|
token
::
Literal
(
token
::
Lit
{
kind
:
token
::
StrRaw
(
..
),
symbol
,
suffix
})
=>
{
self
.expect_no_suffix
(
self
.token.span
,
"an ABI spec"
,
suffix
);
self
.bump
();
Ok
(
Some
(
Abi
::
new
(
symbol
,
self
.prev_span
)))
/// Parses a string literal as an ABI spec.
/// If one is not found, the "C" ABI is used.
fn
parse_opt_abi
(
&
mut
self
)
->
PResult
<
'a
,
Abi
>
{
let
span
=
if
self
.token
.can_begin_literal_or_bool
()
{
let
ast
::
Lit
{
span
,
kind
,
..
}
=
self
.parse_lit
()
?
;
match
kind
{
ast
::
LitKind
::
Str
(
symbol
,
_
)
=>
return
Ok
(
Abi
::
new
(
symbol
,
span
)),
ast
::
LitKind
::
Err
(
_
)
=>
{}
_
=>
{
self
.struct_span_err
(
span
,
"non-string ABI literal"
)
.span_suggestion
(
span
,
"specify the ABI with a string literal"
,
"
\"
C
\"
"
.to_string
(),
Applicability
::
MaybeIncorrect
,
)
.emit
();
}
}
_
=>
Ok
(
None
),
}
span
}
else
{
self
.prev_span
};
Ok
(
Abi
::
new
(
sym
::
C
,
span
))
}
/// We are parsing `async fn`. If we are on Rust 2015, emit an error.
...
...
src/libsyntax/parse/parser/expr.rs
浏览文件 @
1db4d607
...
...
@@ -1116,7 +1116,11 @@ pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
Err
(
self
.span_fatal
(
token
.span
,
&
msg
))
}
Err
(
err
)
=>
{
let
(
lit
,
span
)
=
(
token
.expect_lit
(),
token
.span
);
let
span
=
token
.span
;
let
lit
=
match
token
.kind
{
token
::
Literal
(
lit
)
=>
lit
,
_
=>
unreachable!
(),
};
self
.bump
();
self
.error_literal_from_token
(
err
,
lit
,
span
);
// Pack possible quotes and prefixes from the original literal into
...
...
src/libsyntax/parse/parser/item.rs
浏览文件 @
1db4d607
...
...
@@ -110,7 +110,7 @@ fn parse_item_implementation(
return
Ok
(
Some
(
self
.parse_item_extern_crate
(
lo
,
vis
,
attrs
)
?
));
}
let
abi
=
self
.parse_opt_abi
()
?
.unwrap_or_else
(||
Abi
::
new
(
sym
::
C
,
extern_sp
))
;
let
abi
=
self
.parse_opt_abi
()
?
;
if
self
.eat_keyword
(
kw
::
Fn
)
{
// EXTERN FUNCTION ITEM
...
...
src/libsyntax/parse/token.rs
浏览文件 @
1db4d607
...
...
@@ -402,13 +402,6 @@ pub fn is_lit(&self) -> bool {
}
}
crate
fn
expect_lit
(
&
self
)
->
Lit
{
match
self
.kind
{
Literal
(
lit
)
=>
lit
,
_
=>
panic!
(
"`expect_lit` called on non-literal"
),
}
}
/// Returns `true` if the token is any literal, a minus (which can prefix a literal,
/// for example a '-42', or one of the boolean idents).
pub
fn
can_begin_literal_or_bool
(
&
self
)
->
bool
{
...
...
src/test/ui/parser/bad-lit-suffixes.rs
浏览文件 @
1db4d607
extern
"C"
suffix
//~ ERROR suffixes on a
n ABI spec
are invalid
"C"
suffix
//~ ERROR suffixes on a
string literal
are invalid
fn
foo
()
{}
extern
"C"
suffix
//~ ERROR suffixes on a
n ABI spec
are invalid
"C"
suffix
//~ ERROR suffixes on a
string literal
are invalid
{}
fn
main
()
{
...
...
src/test/ui/parser/bad-lit-suffixes.stderr
浏览文件 @
1db4d607
error: suffixes on a
n ABI spec
are invalid
error: suffixes on a
string literal
are invalid
--> $DIR/bad-lit-suffixes.rs:2:5
|
LL | "C"suffix
| ^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a
n ABI spec
are invalid
error: suffixes on a
string literal
are invalid
--> $DIR/bad-lit-suffixes.rs:6:5
|
LL | "C"suffix
...
...
src/test/ui/parser/extern-abi-from-mac-literal-frag.rs
0 → 100644
浏览文件 @
1db4d607
// check-pass
// In this test we check that the parser accepts an ABI string when it
// comes from a macro `literal` fragment as opposed to a hardcoded string.
fn
main
()
{}
macro_rules!
abi_from_lit_frag
{
(
$abi:literal
)
=>
{
extern
$abi
{
fn
_
import
();
}
extern
$abi
fn
_
export
()
{}
type
_
PTR
=
extern
$abi
fn
();
}
}
mod
rust
{
abi_from_lit_frag!
(
"Rust"
);
}
mod
c
{
abi_from_lit_frag!
(
"C"
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录