Skip to content

Commit d75b0ae

Browse files
committed
Add AST pretty-printer tests involving attr precedence
This test currently fails (as expected). --- stderr ------------------------------- Pretty-printer lost necessary parentheses BEFORE: (#[attr] loop {}).field AFTER: #[attr] loop {}.field ------------------------------------------
1 parent 8da6239 commit d75b0ae

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ static EXPRS: &[&str] = &[
8888
// expressions.
8989
"match 2 { _ => 1 - 1 }",
9090
"match 2 { _ => ({ 1 }) - 1 }",
91+
// Expressions with an outer attr have lower precedence than expressions
92+
// with an inner attr.
93+
"#[attr] loop {}.field",
94+
"(#[attr] loop {}).field",
95+
"loop { #![attr] }.field",
9196
// Grammar restriction: break value starting with a labeled loop is not
9297
// allowed, except if the break is also labeled.
9398
"break 'outer 'inner: loop {} + 2",

0 commit comments

Comments
 (0)