Skip to content

Insert parentheses around binary operation with attribute #142476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,43 @@ impl<'a> State<'a> {

let ib = self.ibox(INDENT_UNIT);

// The Match subexpression in `match x {} - 1` must be parenthesized if
// it is the leftmost subexpression in a statement:
//
// (match x {}) - 1;
//
// But not otherwise:
//
// let _ = match x {} - 1;
//
// Same applies to a small set of other expression kinds which eagerly
// terminate a statement which opens with them.
let needs_par = fixup.would_cause_statement_boundary(expr);
let needs_par = {
// The Match subexpression in `match x {} - 1` must be parenthesized
// if it is the leftmost subexpression in a statement:
//
// (match x {}) - 1;
//
// But not otherwise:
//
// let _ = match x {} - 1;
//
// Same applies to a small set of other expression kinds which
// eagerly terminate a statement which opens with them.
fixup.would_cause_statement_boundary(expr)
} || {
// If a binary operation ends up with an attribute, such as
// resulting from the following macro expansion, then parentheses
// are required so that the attribute encompasses the right
// subexpression and not just the left one.
//
// #![feature(stmt_expr_attributes)]
//
// macro_rules! add_attr {
// ($e:expr) => { #[attr] $e };
// }
//
// let _ = add_attr!(1 + 1);
//
// We must pretty-print `#[attr] (1 + 1)` not `#[attr] 1 + 1`.
!attrs.is_empty()
&& matches!(
expr.kind,
ast::ExprKind::Binary(..)
| ast::ExprKind::Cast(..)
| ast::ExprKind::Assign(..)
| ast::ExprKind::AssignOp(..)
)
};
if needs_par {
self.popen();
fixup = FixupContext::default();
Expand Down
42 changes: 38 additions & 4 deletions tests/ui-fulldeps/auxiliary/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ extern crate rustc_parse;
extern crate rustc_session;
extern crate rustc_span;

use rustc_ast::ast::{DUMMY_NODE_ID, Expr};
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::ast::{AttrKind, Attribute, DUMMY_NODE_ID, Expr};
use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::node_id::NodeId;
use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree, LazyAttrTokenStream};
use rustc_errors::Diag;
use rustc_parse::parser::Recovery;
use rustc_session::parse::ParseSess;
use rustc_span::{DUMMY_SP, FileName, Span};
use rustc_span::{AttrId, DUMMY_SP, FileName, Span};
use std::sync::Arc;

pub fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> {
let parser = rustc_parse::unwrap_or_emit_fatal(rustc_parse::new_parser_from_source_str(
Expand Down Expand Up @@ -46,4 +48,36 @@ impl MutVisitor for Normalize {
fn visit_span(&mut self, span: &mut Span) {
*span = DUMMY_SP;
}

fn visit_attribute(&mut self, attr: &mut Attribute) {
attr.id = AttrId::from_u32(0);
if let AttrKind::Normal(normal_attr) = &mut attr.kind {
if let Some(tokens) = &mut normal_attr.tokens {
let mut stream = tokens.to_attr_token_stream();
normalize_attr_token_stream(&mut stream);
*tokens = LazyAttrTokenStream::new_direct(stream);
}
}
mut_visit::walk_attribute(self, attr);
}
}

fn normalize_attr_token_stream(stream: &mut AttrTokenStream) {
Arc::make_mut(&mut stream.0)
.iter_mut()
.for_each(normalize_attr_token_tree);
}

fn normalize_attr_token_tree(token: &mut AttrTokenTree) {
match token {
AttrTokenTree::Token(token, _spacing) => {
Normalize.visit_span(&mut token.span);
}
AttrTokenTree::Delimited(dspan, _spacing, _delim, stream) => {
normalize_attr_token_stream(stream);
Normalize.visit_span(&mut dspan.open);
Normalize.visit_span(&mut dspan.close);
}
AttrTokenTree::AttrsTarget(_) => unimplemented!("AttrTokenTree::AttrsTarget"),
}
}
17 changes: 17 additions & 0 deletions tests/ui-fulldeps/pprust-parenthesis-insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ static EXPRS: &[&str] = &[
// expressions.
"match 2 { _ => 1 - 1 }",
"match 2 { _ => ({ 1 }) - 1 }",
// Attributes on a Binary, Cast, Assign, and AssignOp expression require
// parentheses.
"#[attr] (1 + 1)",
"#[attr] (1 as T)",
"#[attr] (x = 1)",
"#[attr] (x += 1)",
// If the attribute were not present on the binary operation, it would be
// legal to render this without not just the inner parentheses, but also the
// outer ones. `return x + .. .field` (Yes, really.) Currently the
// pretty-printer does not take advantage of this edge case.
"(return #[attr] (x + ..)).field",
"(return x + ..).field",
// Grammar restriction: break value starting with a labeled loop is not
// allowed, except if the break is also labeled.
"break 'outer 'inner: loop {} + 2",
Expand Down Expand Up @@ -154,7 +166,12 @@ struct Unparenthesize;
impl MutVisitor for Unparenthesize {
fn visit_expr(&mut self, e: &mut P<Expr>) {
while let ExprKind::Paren(paren) = &mut e.kind {
let paren_attrs = mem::take(&mut e.attrs);
*e = mem::replace(paren, Expr::dummy());
if !paren_attrs.is_empty() {
assert!(e.attrs.is_empty());
e.attrs = paren_attrs;
}
}
mut_visit::walk_expr(self, e);
}
Expand Down
Loading