diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2025-06-15 23:51:54 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-15 23:51:54 +0200 | 
| commit | b79d3b1ec15c64bf00677207287b17e3d6f6e05f (patch) | |
| tree | 10b4b523937e18bf1aaed71cb86d5916128db66d /compiler/rustc_lint/src | |
| parent | d6dc9656ea43fed1f862b36a68caffd631194654 (diff) | |
| parent | 2171f89eb223a4f5f3af74df24104d77c1a8a6aa (diff) | |
| download | rust-b79d3b1ec15c64bf00677207287b17e3d6f6e05f.tar.gz rust-b79d3b1ec15c64bf00677207287b17e3d6f6e05f.zip | |
Rollup merge of #134661 - dtolnay:prefixattr, r=fmease
Reduce precedence of expressions that have an outer attr
Previously, `-Zunpretty=expanded` would expand this program as follows:
```rust
#![feature(stmt_expr_attributes)]
macro_rules! repro {
    ($e:expr) => {
        #[allow(deprecated)] $e
    };
}
#[derive(Default)]
struct Thing {
    #[deprecated]
    field: i32,
}
fn main() {
    let thing = Thing::default();
    let _ = repro!(thing).field;
}
```
```rs
#![feature(prelude_import)]
#![feature(stmt_expr_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
struct Thing {
    #[deprecated]
    field: i32,
}
#[automatically_derived]
impl ::core::default::Default for Thing {
    #[inline]
    fn default() -> Thing {
        Thing { field: ::core::default::Default::default() }
    }
}
fn main() {
    let thing = Thing::default();
    let _ = #[allow(deprecated)] thing.field;
}
```
This is not the correct expansion. The correct output would have `(#[allow(deprecated)] thing).field` with the attribute applying only to `thing`, not to `thing.field`.
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/context.rs | 15 | 
1 files changed, 15 insertions, 0 deletions
| diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 5679d4566dc..b6bf45dfbcf 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -7,6 +7,7 @@ use std::cell::Cell; use std::slice; use rustc_ast::BindingMode; +use rustc_ast::util::parser::ExprPrecedence; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::sync; use rustc_data_structures::unord::UnordMap; @@ -850,6 +851,20 @@ impl<'tcx> LateContext<'tcx> { }) } + /// Returns the effective precedence of an expression for the purpose of + /// rendering diagnostic. This is not the same as the precedence that would + /// be used for pretty-printing HIR by rustc_hir_pretty. + pub fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence { + let for_each_attr = |id: hir::HirId, callback: &mut dyn FnMut(&hir::Attribute)| { + for attr in self.tcx.hir_attrs(id) { + if attr.span().desugaring_kind().is_none() { + callback(attr); + } + } + }; + expr.precedence(&for_each_attr) + } + /// If the given expression is a local binding, find the initializer expression. /// If that initializer expression is another local binding, find its initializer again. /// | 
