From 180c6379b8ed8b8ff5d9545c716a17d2225c915c Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Mon, 3 Aug 2026 23:21:15 +0200 Subject: [PATCH] Remove rustc_middle dependency on rustc_hir_pretty There is a `impl PpAnn for TyCtxt` that is unneeded. None of the big crates (middle, trait_selection) actually do any hir pretty printing so it can be removed and can either be implemented for local structs elsewhere or done by casting to `&dyn PpAnn` instead. --- Cargo.lock | 2 +- compiler/rustc_driver_impl/Cargo.toml | 1 + compiler/rustc_driver_impl/src/pretty.rs | 10 +- compiler/rustc_hir_typeck/src/_match.rs | 2 +- compiler/rustc_hir_typeck/src/callee.rs | 2 +- compiler/rustc_hir_typeck/src/expr.rs | 5 +- compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs | 10 + .../src/fn_ctxt/suggestions.rs | 4 +- compiler/rustc_hir_typeck/src/lib.rs | 244 +++++++++--------- compiler/rustc_hir_typeck/src/pat.rs | 17 +- compiler/rustc_middle/Cargo.toml | 1 - compiler/rustc_middle/src/hir/map.rs | 7 - .../rustc_public_bridge/src/context/impls.rs | 14 +- src/librustdoc/json/conversions.rs | 8 +- .../src/matches/match_wild_err_arm.rs | 3 +- .../src/unnecessary_mut_passed.rs | 5 +- 16 files changed, 180 insertions(+), 155 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76b17e02c2359..2190fa22b77ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3954,6 +3954,7 @@ dependencies = [ "rustc_errors", "rustc_expand", "rustc_feature", + "rustc_hir", "rustc_hir_analysis", "rustc_hir_pretty", "rustc_index", @@ -4414,7 +4415,6 @@ dependencies = [ "rustc_graphviz", "rustc_hashes", "rustc_hir", - "rustc_hir_pretty", "rustc_index", "rustc_lint_defs", "rustc_macros", diff --git a/compiler/rustc_driver_impl/Cargo.toml b/compiler/rustc_driver_impl/Cargo.toml index c7d3e4fae3fc5..4871c7eb9e8b0 100644 --- a/compiler/rustc_driver_impl/Cargo.toml +++ b/compiler/rustc_driver_impl/Cargo.toml @@ -16,6 +16,7 @@ rustc_data_structures = { path = "../rustc_data_structures" } rustc_errors = { path = "../rustc_errors" } rustc_expand = { path = "../rustc_expand" } rustc_feature = { path = "../rustc_feature" } +rustc_hir = { path = "../rustc_hir" } rustc_hir_analysis = { path = "../rustc_hir_analysis" } rustc_hir_pretty = { path = "../rustc_hir_pretty" } rustc_index = { path = "../rustc_index" } diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 3a0a6687dd812..4bf1a3d875866 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -7,7 +7,9 @@ use std::io; use rustc_ast as ast; use rustc_ast_pretty::pprust as pprust_ast; +use rustc_hir::intravisit; use rustc_hir_pretty as pprust_hir; +use rustc_hir_pretty::PpAnn; use rustc_middle::bug; use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::ty::{self, TyCtxt}; @@ -71,7 +73,8 @@ struct HirIdentifiedAnn<'tcx> { impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { - self.tcx.nested(state, nested) + let this = &self.tcx as &dyn intravisit::HirTyCtxt<'_>; + this.nested(state, nested) } fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { @@ -149,11 +152,12 @@ struct HirTypedAnn<'tcx> { impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { + let this = &self.tcx as &dyn intravisit::HirTyCtxt<'_>; let old_maybe_typeck_results = self.maybe_typeck_results.get(); if let pprust_hir::Nested::Body(id) = nested { self.maybe_typeck_results.set(Some(self.tcx.typeck_body(id))); } - self.tcx.nested(state, nested); + this.nested(state, nested); self.maybe_typeck_results.set(old_maybe_typeck_results); } @@ -281,7 +285,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { ) }; match s { - PpHirMode::Normal => f(&tcx), + PpHirMode::Normal => f(&(&tcx as &dyn intravisit::HirTyCtxt<'_>) as &dyn PpAnn), PpHirMode::Identified => { let annotation = HirIdentifiedAnn { tcx }; f(&annotation) diff --git a/compiler/rustc_hir_typeck/src/_match.rs b/compiler/rustc_hir_typeck/src/_match.rs index ebf9907e64e64..a1ff036574fdc 100644 --- a/compiler/rustc_hir_typeck/src/_match.rs +++ b/compiler/rustc_hir_typeck/src/_match.rs @@ -421,7 +421,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return self.get_fn_decl(hir_id).map(|(_, fn_decl)| { let (ty, span) = match fn_decl.output { hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span), - hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span), + hir::FnRetTy::Return(ty) => (ty_to_string(self, ty), ty.span), }; (span, format!("expected `{ty}` because of this return type")) }); diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 288a1903bf675..3074a5900773d 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -903,7 +903,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let removal_span = callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi()); unit_variant = - Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(&self.tcx, qpath))); + Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(self, qpath))); } let callee_ty = self.resolve_vars_if_possible(callee_ty); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index f89d67eced3fb..12e7f82cadd43 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -50,7 +50,7 @@ use crate::diagnostics::{ use crate::op::contains_let_in_chain; use crate::{ BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs, - TupleArgumentsFlag, cast, fatally_break_rust, report_unexpected_variant_res, type_error_struct, + TupleArgumentsFlag, cast, fatally_break_rust, type_error_struct, }; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { @@ -589,8 +589,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Ty::new_error(tcx, e) } Res::Def(DefKind::Variant, _) => { - let e = report_unexpected_variant_res( - tcx, + let e = self.report_unexpected_variant_res( res, Some(expr), &[], diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index 287e3857087e7..7a5eeccb98260 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -220,6 +220,16 @@ impl<'a, 'tcx> Deref for FnCtxt<'a, 'tcx> { } } +impl<'tcx> rustc_hir_pretty::PpAnn for FnCtxt<'_, 'tcx> { + fn nested(&self, state: &mut rustc_hir_pretty::State<'_>, nested: rustc_hir_pretty::Nested) { + rustc_hir_pretty::PpAnn::nested( + &(&self.tcx as &dyn rustc_hir::intravisit::HirTyCtxt<'_>), + state, + nested, + ) + } +} + impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> { fn tcx(&self) -> TyCtxt<'tcx> { self.tcx diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index b28eb8ad940d9..fc99dd67289bb 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -723,12 +723,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let hir::FnDecl { inputs, output, .. } = fn_ptr_ty.decl; let inputs_str = - inputs.iter().map(|ty| rustc_hir_pretty::ty_to_string(&self.tcx, ty)).join(", "); + inputs.iter().map(|ty| rustc_hir_pretty::ty_to_string(self, ty)).join(", "); let output_str = match output { hir::FnRetTy::DefaultReturn(_) => String::new(), hir::FnRetTy::Return(ty) => { - format!(" -> {}", rustc_hir_pretty::ty_to_string(&self.tcx, ty)) + format!(" -> {}", rustc_hir_pretty::ty_to_string(self, ty)) } }; diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index c67b8f7cdaf5c..d20d8375fc228 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -478,134 +478,138 @@ impl<'tcx> EnclosingBreakables<'tcx> { } } } - -fn report_unexpected_variant_res( - tcx: TyCtxt<'_>, - res: Res, - expr: Option<&hir::Expr<'_>>, - sub_pats: &[hir::Pat<'_>], - qpath: &hir::QPath<'_>, - span: Span, - err_code: ErrCode, - expected: &str, -) -> ErrorGuaranteed { - let res_descr = match res { - Res::Def(DefKind::Variant, _) => "struct variant", - _ => res.descr(), - }; - let path_str = rustc_hir_pretty::qpath_to_string(&tcx, qpath); - let mut err = tcx - .dcx() - .struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`")) - .with_code(err_code); - match res { - Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == E0164 => { - let patterns_url = "https://doc.rust-lang.org/book/ch19-00-patterns.html"; - err.with_span_label(span, "`fn` calls are not allowed in patterns") - .with_help(format!("for more information, visit {patterns_url}")) - } - Res::Def(DefKind::Variant, _) if let Some(expr) = expr => { - err.span_label(span, format!("not a {expected}")); - let variant = tcx.expect_variant_res(res); - let sugg = if variant.fields.is_empty() { - " {}".to_string() - } else { - format!( - " {{ {} }}", - variant - .fields - .iter() - .map(|f| format!("{}: /* value */", f.name)) - .collect::>() - .join(", ") - ) - }; - let descr = "you might have meant to create a new value of the struct"; - let mut suggestion = vec![]; - match tcx.parent_hir_node(expr.hir_id) { - hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Call(..), - span: call_span, - .. - }) => { - suggestion.push((span.shrink_to_hi().with_hi(call_span.hi()), sugg)); - } - hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(..), hir_id, .. }) => { - suggestion.push((expr.span.shrink_to_lo(), "(".to_string())); - if let hir::Node::Expr(parent) = tcx.parent_hir_node(*hir_id) - && let hir::ExprKind::If(condition, block, None) = parent.kind - && condition.hir_id == *hir_id - && let hir::ExprKind::Block(block, _) = block.kind - && block.stmts.is_empty() - && let Some(expr) = block.expr - && let hir::ExprKind::Path(..) = expr.kind - { - // Special case: you can incorrectly write an equality condition: - // if foo == Struct { field } { /* if body */ } - // which should have been written - // if foo == (Struct { field }) { /* if body */ } - suggestion.push((block.span.shrink_to_hi(), ")".to_string())); - } else { - suggestion.push((span.shrink_to_hi().with_hi(expr.span.hi()), sugg)); +impl<'a, 'tcx> FnCtxt<'a, 'tcx> { + fn report_unexpected_variant_res( + &self, + res: Res, + expr: Option<&hir::Expr<'_>>, + sub_pats: &[hir::Pat<'_>], + qpath: &hir::QPath<'_>, + span: Span, + err_code: ErrCode, + expected: &str, + ) -> ErrorGuaranteed { + let tcx = self.tcx; + let res_descr = match res { + Res::Def(DefKind::Variant, _) => "struct variant", + _ => res.descr(), + }; + let path_str = rustc_hir_pretty::qpath_to_string(self, qpath); + let mut err = tcx + .dcx() + .struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`")) + .with_code(err_code); + match res { + Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == E0164 => { + let patterns_url = "https://doc.rust-lang.org/book/ch19-00-patterns.html"; + err.with_span_label(span, "`fn` calls are not allowed in patterns") + .with_help(format!("for more information, visit {patterns_url}")) + } + Res::Def(DefKind::Variant, _) if let Some(expr) = expr => { + err.span_label(span, format!("not a {expected}")); + let variant = tcx.expect_variant_res(res); + let sugg = if variant.fields.is_empty() { + " {}".to_string() + } else { + format!( + " {{ {} }}", + variant + .fields + .iter() + .map(|f| format!("{}: /* value */", f.name)) + .collect::>() + .join(", ") + ) + }; + let descr = "you might have meant to create a new value of the struct"; + let mut suggestion = vec![]; + match tcx.parent_hir_node(expr.hir_id) { + hir::Node::Expr(hir::Expr { + kind: hir::ExprKind::Call(..), + span: call_span, + .. + }) => { + suggestion.push((span.shrink_to_hi().with_hi(call_span.hi()), sugg)); + } + hir::Node::Expr(hir::Expr { + kind: hir::ExprKind::Binary(..), hir_id, .. + }) => { + suggestion.push((expr.span.shrink_to_lo(), "(".to_string())); + if let hir::Node::Expr(parent) = tcx.parent_hir_node(*hir_id) + && let hir::ExprKind::If(condition, block, None) = parent.kind + && condition.hir_id == *hir_id + && let hir::ExprKind::Block(block, _) = block.kind + && block.stmts.is_empty() + && let Some(expr) = block.expr + && let hir::ExprKind::Path(..) = expr.kind + { + // Special case: you can incorrectly write an equality condition: + // if foo == Struct { field } { /* if body */ } + // which should have been written + // if foo == (Struct { field }) { /* if body */ } + suggestion.push((block.span.shrink_to_hi(), ")".to_string())); + } else { + suggestion.push((span.shrink_to_hi().with_hi(expr.span.hi()), sugg)); + } + } + _ => { + suggestion.push((span.shrink_to_hi(), sugg)); } } - _ => { - suggestion.push((span.shrink_to_hi(), sugg)); - } + + err.multipart_suggestion(descr, suggestion, Applicability::HasPlaceholders); + err } + Res::Def(DefKind::Variant, _) if expr.is_none() => { + err.span_label(span, format!("not a {expected}")); - err.multipart_suggestion(descr, suggestion, Applicability::HasPlaceholders); - err - } - Res::Def(DefKind::Variant, _) if expr.is_none() => { - err.span_label(span, format!("not a {expected}")); - - let fields = &tcx.expect_variant_res(res).fields.raw; - let span = qpath.span().shrink_to_hi().to(span.shrink_to_hi()); - let (msg, sugg) = if fields.is_empty() { - ("use the struct variant pattern syntax", " {}".to_string()) - } else { - let msg = if fields.is_empty() { - "use struct variant pattern syntax" + let fields = &tcx.expect_variant_res(res).fields.raw; + let span = qpath.span().shrink_to_hi().to(span.shrink_to_hi()); + let (msg, sugg) = if fields.is_empty() { + ("use the struct variant pattern syntax", " {}".to_string()) } else { - "add the names to match a struct variant's fields" + let msg = if fields.is_empty() { + "use struct variant pattern syntax" + } else { + "add the names to match a struct variant's fields" + }; + let fields_sugg = fields + .iter() + .enumerate() + .map(|(i, field)| { + let field_name = field.ident(tcx).to_string(); + + let pat_snippet = sub_pats + .get(i) + .and_then(|sub_pat| { + tcx.sess.source_map().span_to_snippet(sub_pat.span).ok() + }) + .unwrap_or_else(|| "_".to_string()); + + if field_name == pat_snippet { + field_name + } else { + format!("{field_name}: {pat_snippet}") + } + }) + .collect::>() + .join(", "); + let sugg = format!(" {{ {} }}", fields_sugg); + (msg, sugg) }; - let fields_sugg = fields - .iter() - .enumerate() - .map(|(i, field)| { - let field_name = field.ident(tcx).to_string(); - - let pat_snippet = sub_pats - .get(i) - .and_then(|sub_pat| { - tcx.sess.source_map().span_to_snippet(sub_pat.span).ok() - }) - .unwrap_or_else(|| "_".to_string()); - - if field_name == pat_snippet { - field_name - } else { - format!("{field_name}: {pat_snippet}") - } - }) - .collect::>() - .join(", "); - let sugg = format!(" {{ {} }}", fields_sugg); - (msg, sugg) - }; - - err.span_suggestion_verbose( - qpath.span().shrink_to_hi().to(span.shrink_to_hi()), - msg, - sugg, - Applicability::HasPlaceholders, - ); - err + + err.span_suggestion_verbose( + qpath.span().shrink_to_hi().to(span.shrink_to_hi()), + msg, + sugg, + Applicability::HasPlaceholders, + ); + err + } + _ => err.with_span_label(span, format!("not a {expected}")), } - _ => err.with_span_label(span, format!("not a {expected}")), + .emit() } - .emit() } /// Controls whether all arguments are tupled. This is used for the call operator only. diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 52602b8041d66..01c48c0ae790c 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -32,7 +32,6 @@ use tracing::{debug, instrument, trace}; use ty::VariantDef; use ty::adjustment::{PatAdjust, PatAdjustment}; -use super::report_unexpected_variant_res; use crate::expectation::Expectation; use crate::gather_locals::DeclOrigin; use crate::{FnCtxt, diagnostics}; @@ -1585,8 +1584,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => { let expected = "unit struct, unit variant or constant"; - let e = report_unexpected_variant_res( - tcx, + let e = self.report_unexpected_variant_res( res, None, &[], @@ -1604,8 +1602,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { // Ok, we allow unit struct ctors in patterns only. } else { - let e = report_unexpected_variant_res( - tcx, + let e = self.report_unexpected_variant_res( res, None, &[], @@ -1775,8 +1772,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir::PatKind::TupleStruct(_, sub_pats, _) => sub_pats, _ => &[], }; - let e = report_unexpected_variant_res( - tcx, res, None, sub_pats, qpath, pat.span, E0164, expected, + let e = self.report_unexpected_variant_res( + res, None, sub_pats, qpath, pat.span, E0164, expected, ); Err(e) }; @@ -2237,7 +2234,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { let has_shorthand_field_name = field_patterns.iter().any(|field| field.is_shorthand); if has_shorthand_field_name { - let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath); + let path = rustc_hir_pretty::qpath_to_string(self, qpath); let mut err = struct_span_code_err!( self.dcx(), pat.span, @@ -2422,7 +2419,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // we don't care to report errors for a struct if the struct itself is tainted variant.has_errors()?; - let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath); + let path = rustc_hir_pretty::qpath_to_string(self, qpath); let mut err = struct_span_code_err!( self.dcx(), pat.span, @@ -2472,7 +2469,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { f } } - Err(_) => rustc_hir_pretty::pat_to_string(&self.tcx, field.pat), + Err(_) => rustc_hir_pretty::pat_to_string(self, field.pat), } }) .collect::>() diff --git a/compiler/rustc_middle/Cargo.toml b/compiler/rustc_middle/Cargo.toml index f624fcee78f59..55608083d3751 100644 --- a/compiler/rustc_middle/Cargo.toml +++ b/compiler/rustc_middle/Cargo.toml @@ -22,7 +22,6 @@ rustc_feature = { path = "../rustc_feature" } rustc_graphviz = { path = "../rustc_graphviz" } rustc_hashes = { path = "../rustc_hashes" } rustc_hir = { path = "../rustc_hir" } -rustc_hir_pretty = { path = "../rustc_hir_pretty" } rustc_index = { path = "../rustc_index" } rustc_lint_defs = { path = "../rustc_lint_defs" } rustc_macros = { path = "../rustc_macros" } diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index c01d9e98e9b9c..8ec27921a5787 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -15,7 +15,6 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_hir::intravisit::Visitor; use rustc_hir::lints::DelayedLints; use rustc_hir::*; -use rustc_hir_pretty as pprust_hir; use rustc_span::def_id::{CRATE_MOD_ID, StableCrateId}; use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans}; @@ -1156,12 +1155,6 @@ impl<'tcx> intravisit::HirTyCtxt<'tcx> for TyCtxt<'tcx> { } } -impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> { - fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { - pprust_hir::PpAnn::nested(&(self as &dyn intravisit::HirTyCtxt<'_>), state, nested) - } -} - pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh { let krate = tcx.hir_crate_items(()); let upstream_crates = upstream_crates(tcx); diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index 4a2fbb8f8b7af..f648a85249dfc 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -52,6 +52,16 @@ impl<'tcx, B: Bridge> AllocRangeHelpers<'tcx> for CompilerCtxt<'tcx, B> { } } +impl<'tcx, B: Bridge> rustc_hir_pretty::PpAnn for CompilerCtxt<'tcx, B> { + fn nested(&self, state: &mut rustc_hir_pretty::State<'_>, nested: rustc_hir_pretty::Nested) { + rustc_hir_pretty::PpAnn::nested( + &(&self.tcx as &dyn rustc_hir::intravisit::HirTyCtxt<'_>), + state, + nested, + ) + } +} + impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { pub fn lift>>(&self, value: T) -> T::Lifted { self.tcx.lift(value) @@ -295,7 +305,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { .get_attrs_by_path(def_id, &attr_name) .filter_map(|attribute| { if let Attribute::Unparsed(u) = attribute { - let attr_str = rustc_hir_pretty::attribute_to_string(&self.tcx, attribute); + let attr_str = rustc_hir_pretty::attribute_to_string(self, attribute); Some((attr_str, u.span)) } else { None @@ -314,7 +324,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { attrs_iter .filter_map(|attribute| { if let Attribute::Unparsed(u) = attribute { - let attr_str = rustc_hir_pretty::attribute_to_string(&self.tcx, attribute); + let attr_str = rustc_hir_pretty::attribute_to_string(self, attribute); Some((attr_str, u.span)) } else { None diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 7e46b2f593e49..eb382f368905f 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -12,7 +12,8 @@ use rustc_hir::attrs::{ }; use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::DefId; -use rustc_hir::{HeaderSafety, Safety, find_attr}; +use rustc_hir::{HeaderSafety, Safety, find_attr, intravisit}; +use rustc_hir_pretty::PpAnn; use rustc_metadata::rendered_const; use rustc_middle::ty::TyCtxt; use rustc_middle::{bug, ty}; @@ -1243,7 +1244,10 @@ fn maybe_from_hir_attr(attr: &hir::Attribute, item_id: ItemId, tcx: TyCtxt<'_>) } fn other_attr(tcx: TyCtxt<'_>, attr: &hir::Attribute) -> Attribute { - let mut s = rustc_hir_pretty::attribute_to_string(&tcx, attr); + let mut s = rustc_hir_pretty::attribute_to_string( + &(&tcx as &dyn intravisit::HirTyCtxt<'_>) as &dyn PpAnn, + attr, + ); assert_eq!(s.pop(), Some('\n')); Attribute::Other(s) } diff --git a/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs b/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs index e38ba801c0bf7..9fc9f9944465c 100644 --- a/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs +++ b/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs @@ -6,6 +6,7 @@ use clippy_utils::{is_in_const_context, is_wild, peel_blocks_with_stmt}; use rustc_hir::{Arm, Expr, PatKind}; use rustc_lint::LateContext; use rustc_span::symbol::{kw, sym}; +use rustc_hir::intravisit; use super::MATCH_WILD_ERR_ARM; @@ -19,7 +20,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<' if ex_ty.is_diag_item(cx, sym::Result) { for arm in arms { if let PatKind::TupleStruct(ref path, inner, _) = arm.pat.kind { - let path_str = rustc_hir_pretty::qpath_to_string(&cx.tcx, path); + let path_str = rustc_hir_pretty::qpath_to_string(#[allow(trivial_casts)] &(&cx.tcx as &dyn intravisit::HirTyCtxt<'_>), path); if path_str == "Err" { let mut matching_wild = inner.iter().any(is_wild); let mut ident_bind_name = kw::Underscore; diff --git a/src/tools/clippy/clippy_lints/src/unnecessary_mut_passed.rs b/src/tools/clippy/clippy_lints/src/unnecessary_mut_passed.rs index 60a6688927ab5..43721fa252837 100644 --- a/src/tools/clippy/clippy_lints/src/unnecessary_mut_passed.rs +++ b/src/tools/clippy/clippy_lints/src/unnecessary_mut_passed.rs @@ -6,6 +6,8 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; use rustc_session::declare_lint_pass; use std::iter; +use rustc_hir_pretty::PpAnn; +use rustc_hir::intravisit; declare_clippy_lint! { /// ### What it does @@ -51,7 +53,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed { cx, &mut arguments.iter(), cx.typeck_results().expr_ty(fn_expr), - &rustc_hir_pretty::qpath_to_string(&cx.tcx, path), + #[allow(trivial_casts)] + &rustc_hir_pretty::qpath_to_string(&(&cx.tcx as &dyn intravisit::HirTyCtxt<'_>) as &dyn PpAnn, path), "function", ); }