Skip to content

Commit 215aac6

Browse files
committed
Add AST pretty-printer tests involving attr on binary operation
This test currently fails (as expected). --- stderr ------------------------------- Pretty-printer lost necessary parentheses BEFORE: #[attr] (1 + 1) AFTER: #[attr] 1 + 1 Pretty-printer lost necessary parentheses BEFORE: #[attr] (1 as T) AFTER: #[attr] 1 as T Pretty-printer lost necessary parentheses BEFORE: #[attr] (x = 1) AFTER: #[attr] x = 1 Pretty-printer lost necessary parentheses BEFORE: #[attr] (x += 1) AFTER: #[attr] x += 1 ------------------------------------------
1 parent 497cb89 commit 215aac6

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

tests/ui-fulldeps/auxiliary/parser.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ extern crate rustc_parse;
77
extern crate rustc_session;
88
extern crate rustc_span;
99

10-
use rustc_ast::ast::{DUMMY_NODE_ID, Expr};
11-
use rustc_ast::mut_visit::MutVisitor;
10+
use rustc_ast::ast::{AttrKind, Attribute, DUMMY_NODE_ID, Expr};
11+
use rustc_ast::mut_visit::{self, MutVisitor};
1212
use rustc_ast::node_id::NodeId;
1313
use rustc_ast::ptr::P;
14-
use rustc_ast::token;
14+
use rustc_ast::token::{self, Token};
15+
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree, LazyAttrTokenStream};
1516
use rustc_errors::Diag;
1617
use rustc_parse::parser::Recovery;
1718
use rustc_session::parse::ParseSess;
18-
use rustc_span::{DUMMY_SP, FileName, Span};
19+
use rustc_span::{AttrId, DUMMY_SP, FileName, Span};
20+
use std::sync::Arc;
1921

2022
pub fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> {
2123
let parser = rustc_parse::unwrap_or_emit_fatal(rustc_parse::new_parser_from_source_str(
@@ -46,4 +48,36 @@ impl MutVisitor for Normalize {
4648
fn visit_span(&mut self, span: &mut Span) {
4749
*span = DUMMY_SP;
4850
}
51+
52+
fn visit_attribute(&mut self, attr: &mut Attribute) {
53+
attr.id = AttrId::from_u32(0);
54+
if let AttrKind::Normal(normal_attr) = &mut attr.kind {
55+
if let Some(tokens) = &mut normal_attr.tokens {
56+
let mut stream = tokens.to_attr_token_stream();
57+
normalize_attr_token_stream(&mut stream);
58+
*tokens = LazyAttrTokenStream::new_direct(stream);
59+
}
60+
}
61+
mut_visit::walk_attribute(self, attr);
62+
}
63+
}
64+
65+
fn normalize_attr_token_stream(stream: &mut AttrTokenStream) {
66+
Arc::make_mut(&mut stream.0)
67+
.iter_mut()
68+
.for_each(normalize_attr_token_tree);
69+
}
70+
71+
fn normalize_attr_token_tree(token: &mut AttrTokenTree) {
72+
match token {
73+
AttrTokenTree::Token(token, _spacing) => {
74+
Normalize.visit_span(&mut token.span);
75+
}
76+
AttrTokenTree::Delimited(dspan, _spacing, _delim, stream) => {
77+
normalize_attr_token_stream(stream);
78+
Normalize.visit_span(&mut dspan.open);
79+
Normalize.visit_span(&mut dspan.close);
80+
}
81+
AttrTokenTree::AttrsTarget(_) => unimplemented!("AttrTokenTree::AttrsTarget"),
82+
}
4983
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ static EXPRS: &[&str] = &[
8888
// expressions.
8989
"match 2 { _ => 1 - 1 }",
9090
"match 2 { _ => ({ 1 }) - 1 }",
91+
// Attributes on a Binary, Cast, Assign, and AssignOp expression require
92+
// parentheses.
93+
"#[attr] (1 + 1)",
94+
"#[attr] (1 as T)",
95+
"#[attr] (x = 1)",
96+
"#[attr] (x += 1)",
9197
// Grammar restriction: break value starting with a labeled loop is not
9298
// allowed, except if the break is also labeled.
9399
"break 'outer 'inner: loop {} + 2",

0 commit comments

Comments
 (0)