Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
851066f5
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,发现更多精彩内容 >>
提交
851066f5
编写于
6月 18, 2019
作者:
M
Mazdak Farrokhzad
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
let_chains: Fix bugs in pretty printing.
上级
7465eb44
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
46 addition
and
14 deletion
+46
-14
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+2
-2
src/libsyntax/print/pprust.rs
src/libsyntax/print/pprust.rs
+28
-12
src/libsyntax/util/parser.rs
src/libsyntax/util/parser.rs
+16
-0
未找到文件。
src/libsyntax/parse/parser.rs
浏览文件 @
851066f5
...
...
@@ -41,7 +41,7 @@
use
crate
::
parse
::
lexer
::
comments
::{
doc_comment_style
,
strip_doc_comment_decoration
};
use
crate
::
parse
::
token
::{
Token
,
TokenKind
,
DelimToken
};
use
crate
::
parse
::{
new_sub_parser_from_file
,
ParseSess
,
Directory
,
DirectoryOwnership
};
use
crate
::
util
::
parser
::{
AssocOp
,
Fixity
};
use
crate
::
util
::
parser
::{
AssocOp
,
Fixity
,
prec_let_scrutinee_needs_par
};
use
crate
::
print
::
pprust
;
use
crate
::
ptr
::
P
;
use
crate
::
parse
::
PResult
;
...
...
@@ -3208,7 +3208,7 @@ fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>>
self
.expect
(
&
token
::
Eq
)
?
;
let
expr
=
self
.with_res
(
Restrictions
::
NO_STRUCT_LITERAL
,
|
this
|
this
.parse_assoc_expr_with
(
1
+
AssocOp
::
LAnd
.precedence
(),
None
.into
())
|
this
|
this
.parse_assoc_expr_with
(
1
+
prec_let_scrutinee_needs_par
(),
None
.into
())
)
?
;
let
span
=
lo
.to
(
expr
.span
);
self
.sess.let_chains_spans
.borrow_mut
()
.push
(
span
);
...
...
src/libsyntax/print/pprust.rs
浏览文件 @
851066f5
...
...
@@ -1715,6 +1715,7 @@ pub fn print_block_maybe_unclosed(&mut self,
self
.ann
.post
(
self
,
AnnNode
::
Block
(
blk
))
}
/// Print a `let pats = scrutinee` expression.
pub
fn
print_let
(
&
mut
self
,
pats
:
&
[
P
<
ast
::
Pat
>
],
scrutinee
:
&
ast
::
Expr
)
->
io
::
Result
<
()
>
{
self
.s
.word
(
"let "
)
?
;
...
...
@@ -1722,7 +1723,11 @@ pub fn print_let(&mut self, pats: &[P<ast::Pat>], scrutinee: &ast::Expr) -> io::
self
.s
.space
()
?
;
self
.word_space
(
"="
)
?
;
self
.print_expr_as_cond
(
scrutinee
)
self
.print_expr_cond_paren
(
scrutinee
,
Self
::
cond_needs_par
(
scrutinee
)
||
parser
::
needs_par_as_let_scrutinee
(
scrutinee
.precedence
()
.order
())
)
}
fn
print_else
(
&
mut
self
,
els
:
Option
<&
ast
::
Expr
>
)
->
io
::
Result
<
()
>
{
...
...
@@ -1794,21 +1799,18 @@ fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> io::Result<()> {
}
pub
fn
print_expr_maybe_paren
(
&
mut
self
,
expr
:
&
ast
::
Expr
,
prec
:
i8
)
->
io
::
Result
<
()
>
{
let
needs_par
=
expr
.precedence
()
.order
()
<
prec
;
if
needs_par
{
self
.popen
()
?
;
}
self
.print_expr
(
expr
)
?
;
if
needs_par
{
self
.pclose
()
?
;
}
Ok
(())
self
.print_expr_cond_paren
(
expr
,
expr
.precedence
()
.order
()
<
prec
)
}
/// Print an expr using syntax that's acceptable in a condition position, such as the `cond` in
/// `if cond { ... }`.
pub
fn
print_expr_as_cond
(
&
mut
self
,
expr
:
&
ast
::
Expr
)
->
io
::
Result
<
()
>
{
let
needs_par
=
match
expr
.node
{
self
.print_expr_cond_paren
(
expr
,
Self
::
cond_needs_par
(
expr
))
}
/// Does `expr` need parenthesis when printed in a condition position?
fn
cond_needs_par
(
expr
:
&
ast
::
Expr
)
->
bool
{
match
expr
.node
{
// These cases need parens due to the parse error observed in #26461: `if return {}`
// parses as the erroneous construct `if (return {})`, not `if (return) {}`.
ast
::
ExprKind
::
Closure
(
..
)
|
...
...
@@ -1816,8 +1818,11 @@ pub fn print_expr_as_cond(&mut self, expr: &ast::Expr) -> io::Result<()> {
ast
::
ExprKind
::
Break
(
..
)
=>
true
,
_
=>
parser
::
contains_exterior_struct_lit
(
expr
),
};
}
}
/// Print `expr` or `(expr)` when `needs_par` holds.
fn
print_expr_cond_paren
(
&
mut
self
,
expr
:
&
ast
::
Expr
,
needs_par
:
bool
)
->
io
::
Result
<
()
>
{
if
needs_par
{
self
.popen
()
?
;
}
...
...
@@ -1949,6 +1954,17 @@ fn print_expr_binary(&mut self,
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
(
&
ast
::
ExprKind
::
Cast
{
..
},
ast
::
BinOpKind
::
Lt
)
|
(
&
ast
::
ExprKind
::
Cast
{
..
},
ast
::
BinOpKind
::
Shl
)
=>
parser
::
PREC_FORCE_PAREN
,
// We are given `(let _ = a) OP b`.
//
// - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens
// as the parser will interpret this as `(let _ = a) OP b`.
//
// - Otherwise, e.g. when we have `(let a = b) < c` in AST,
// parens are required since the parser would interpret `let a = b < c` as
// `let a = (b < c)`. To achieve this, we force parens.
(
&
ast
::
ExprKind
::
Let
{
..
},
_
)
if
!
parser
::
needs_par_as_let_scrutinee
(
prec
)
=>
{
parser
::
PREC_FORCE_PAREN
}
_
=>
left_prec
,
};
...
...
src/libsyntax/util/parser.rs
浏览文件 @
851066f5
...
...
@@ -318,6 +318,9 @@ pub fn order(self) -> i8 {
ExprPrecedence
::
Box
|
ExprPrecedence
::
AddrOf
|
// Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
// However, this is not exactly right. When `let _ = a` is the LHS of a binop we
// need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
// but we need to print `(let _ = a) < b` as-is with parens.
ExprPrecedence
::
Let
|
ExprPrecedence
::
Unary
=>
PREC_PREFIX
,
...
...
@@ -352,6 +355,19 @@ pub fn order(self) -> i8 {
}
}
/// In `let p = e`, operators with precedence `<=` this one requires parenthesis in `e`.
crate
fn
prec_let_scrutinee_needs_par
()
->
usize
{
AssocOp
::
LAnd
.precedence
()
}
/// Suppose we have `let _ = e` and the `order` of `e`.
/// Is the `order` such that `e` in `let _ = e` needs parenthesis when it is on the RHS?
///
/// Conversely, suppose that we have `(let _ = a) OP b` and `order` is that of `OP`.
/// Can we print this as `let _ = a OP b`?
crate
fn
needs_par_as_let_scrutinee
(
order
:
i8
)
->
bool
{
order
<=
prec_let_scrutinee_needs_par
()
as
i8
}
/// Expressions that syntactically contain an "exterior" struct literal i.e., not surrounded by any
/// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录