Skip to content

Commit 0a09252

Browse files
authored
Rollup merge of rust-lang#134834 - dtolnay:unnamedcall, r=compiler-errors
Skip parenthesis around tuple struct field calls The pretty-printer previously did not distinguish between named vs unnamed fields when printing a function call containing a struct field. It would print the call as `(self.fun)()` for a named field which is correct, and `(self.0)()` for an unnamed field which is redundant. This PR changes function calls of tuple struct fields to print without parens. **Before:** ```rust struct Tuple(fn()); fn main() { let tuple = Tuple(|| {}); (tuple.0)(); } ``` **After:** ```rust struct Tuple(fn()); fn main() { let tuple = Tuple(|| {}); tuple.0(); } ```
2 parents 3fc0f08 + 26bb4e6 commit 0a09252

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ impl<'a> State<'a> {
213213

214214
fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
215215
let needs_paren = match func.kind {
216-
ast::ExprKind::Field(..) => true,
216+
// In order to call a named field, needs parens: `(self.fun)()`
217+
// But not for an unnamed field: `self.0()`
218+
ast::ExprKind::Field(_, name) => !name.is_numeric(),
217219
_ => func.precedence() < ExprPrecedence::Unambiguous,
218220
};
219221

compiler/rustc_parse/src/parser/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ impl<'a> Parser<'a> {
13361336
) -> bool {
13371337
if let ExprKind::Binary(op, l1, r1) = &inner_op.kind {
13381338
if let ExprKind::Field(_, ident) = l1.kind
1339-
&& ident.as_str().parse::<i32>().is_err()
1339+
&& !ident.is_numeric()
13401340
&& !matches!(r1.kind, ExprKind::Lit(_))
13411341
{
13421342
// The parser has encountered `foo.bar<baz`, the likelihood of the turbofish

compiler/rustc_span/src/symbol.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,12 @@ impl Ident {
27082708
pub fn is_raw_guess(self) -> bool {
27092709
self.name.can_be_raw() && self.is_reserved()
27102710
}
2711+
2712+
/// Whether this would be the identifier for a tuple field like `self.0`, as
2713+
/// opposed to a named field like `self.thing`.
2714+
pub fn is_numeric(self) -> bool {
2715+
!self.name.is_empty() && self.as_str().bytes().all(|b| b.is_ascii_digit())
2716+
}
27112717
}
27122718

27132719
/// Collect all the keywords in a given edition into a vector.

tests/ui-fulldeps/pprust-parenthesis-insertion.rs

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ static EXPRS: &[&str] = &[
7777
// These mean different things.
7878
"if let _ = true && false {}",
7979
"if let _ = (true && false) {}",
80+
// Parentheses to call a named field, but not an unnamed field.
81+
"(self.fun)()",
82+
"self.0()",
8083
// Conditions end at the first curly brace, so struct expressions need to be
8184
// parenthesized. Except in a match guard, where conditions end at arrow.
8285
"if let _ = (Struct {}) {}",

0 commit comments

Comments
 (0)