Skip to content

Commit

Permalink
Unrolled build for rust-lang#123926
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#123926 - compiler-errors:no-ann, r=estebank

Fix pretty HIR for anon consts in diagnostics

This removes the `NoAnn` printer which skips over nested bodies altogether, which is confusing, and requires users of `{ty|qpath|pat}_to_string` to pass in `&tcx` which now impleemnts `hir_pretty::PpAnn`.

There's one case where this "regresses" by actually printing out the body of the anon const -- we could suppress that, but I don't expect people to actually get anon consts like that unless they're fuzzing, tbh.

r? estebank
  • Loading branch information
rust-timer authored Apr 16, 2024
2 parents ccfcd95 + 2beb6a4 commit 56352ad
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4278,6 +4278,7 @@ dependencies = [
"rustc_fluent_macro",
"rustc_graphviz",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
"rustc_macros",
"rustc_query_system",
Expand Down
19 changes: 1 addition & 18 deletions compiler/rustc_driver_impl/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ struct AstNoAnn;

impl pprust_ast::PpAnn for AstNoAnn {}

struct HirNoAnn<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> pprust_hir::PpAnn for HirNoAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
pprust_hir::PpAnn::nested(
&(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
state,
nested,
)
}
}

struct AstIdentifiedAnn;

impl pprust_ast::PpAnn for AstIdentifiedAnn {
Expand Down Expand Up @@ -300,10 +286,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
)
};
match s {
PpHirMode::Normal => {
let annotation = HirNoAnn { tcx };
f(&annotation)
}
PpHirMode::Normal => f(&tcx),
PpHirMode::Identified => {
let annotation = HirIdentifiedAnn { tcx };
f(&annotation)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
let hir_id = expr.hir_id;
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind
&& matches!(borrow_kind, hir::BorrowKind::Ref)
&& let Some(var) = is_path_static_mut(*expr)
&& let Some(var) = path_if_static_mut(tcx, expr)
{
handle_static_mut_ref(tcx, span, var, span.edition().at_least_rust_2024(), m, hir_id);
}
Expand All @@ -24,7 +24,7 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
&& let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind
&& let hir::ByRef::Yes(rmutbl) = ba.0
&& let Some(init) = loc.init
&& let Some(var) = is_path_static_mut(*init)
&& let Some(var) = path_if_static_mut(tcx, init)
{
handle_static_mut_ref(
tcx,
Expand All @@ -37,13 +37,13 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
}
}

fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> {
fn path_if_static_mut(tcx: TyCtxt<'_>, expr: &hir::Expr<'_>) -> Option<String> {
if let hir::ExprKind::Path(qpath) = expr.kind
&& let hir::QPath::Resolved(_, path) = qpath
&& let hir::def::Res::Def(def_kind, _) = path.res
&& let hir::def::DefKind::Static { mutability: Mutability::Mut, nested: false } = def_kind
{
return Some(qpath_to_string(&qpath));
return Some(qpath_to_string(&tcx, &qpath));
}
None
}
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ pub trait PpAnn {
fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
}

pub struct NoAnn;

impl PpAnn for NoAnn {}

impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
fn nested(&self, state: &mut State<'_>, nested: Nested) {
match nested {
Expand Down Expand Up @@ -190,16 +186,16 @@ where
printer.s.eof()
}

pub fn ty_to_string(ty: &hir::Ty<'_>) -> String {
to_string(&NoAnn, |s| s.print_type(ty))
pub fn ty_to_string(ann: &dyn PpAnn, ty: &hir::Ty<'_>) -> String {
to_string(ann, |s| s.print_type(ty))
}

pub fn qpath_to_string(segment: &hir::QPath<'_>) -> String {
to_string(&NoAnn, |s| s.print_qpath(segment, false))
pub fn qpath_to_string(ann: &dyn PpAnn, segment: &hir::QPath<'_>) -> String {
to_string(ann, |s| s.print_qpath(segment, false))
}

pub fn pat_to_string(pat: &hir::Pat<'_>) -> String {
to_string(&NoAnn, |s| s.print_pat(pat))
pub fn pat_to_string(ann: &dyn PpAnn, pat: &hir::Pat<'_>) -> String {
to_string(ann, |s| s.print_pat(pat))
}

impl<'a> State<'a> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,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(ty), ty.span),
hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span),
};
(span, format!("expected `{ty}` because of this return type"))
});
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
def::CtorOf::Variant => "enum variant",
};
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(qpath)));
unit_variant =
Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(&self.tcx, qpath)));
}

let callee_ty = self.resolve_vars_if_possible(callee_ty);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ fn report_unexpected_variant_res(
Res::Def(DefKind::Variant, _) => "struct variant",
_ => res.descr(),
};
let path_str = rustc_hir_pretty::qpath_to_string(qpath);
let path_str = rustc_hir_pretty::qpath_to_string(&tcx, qpath);
let err = tcx
.dcx()
.struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,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(qpath);
let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
let mut err = struct_span_code_err!(
self.dcx(),
pat.span,
Expand Down Expand Up @@ -1743,7 +1743,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return None;
}

let path = rustc_hir_pretty::qpath_to_string(qpath);
let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
let mut err = struct_span_code_err!(
self.dcx(),
pat.span,
Expand Down Expand Up @@ -1793,7 +1793,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
f
}
}
Err(_) => rustc_hir_pretty::pat_to_string(field.pat),
Err(_) => rustc_hir_pretty::pat_to_string(&self.tcx, field.pat),
}
})
.collect::<Vec<String>>()
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rustc_feature = { path = "../rustc_feature" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_graphviz = { path = "../rustc_graphviz" }
rustc_hir = { path = "../rustc_hir" }
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_query_system = { path = "../rustc_query_system" }
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_hir::intravisit::Visitor;
use rustc_hir::*;
use rustc_hir_pretty as pprust_hir;
use rustc_middle::hir::nested_filter;
use rustc_span::def_id::StableCrateId;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
Expand Down Expand Up @@ -999,6 +1000,12 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
}
}

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.hir() as &dyn intravisit::Map<'_>), state, nested)
}
}

pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
let krate = tcx.hir_crate(());
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/casts/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {

let (help, final_suggestion) = if let Some(method) = omit_cast.corresponding_item() {
// don't force absolute path
let method = qpath_to_string(method);
let method = qpath_to_string(&cx.tcx, method);
("try call directly", format!("{method}{turbofish}()"))
} else {
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'
if is_type_diagnostic_item(cx, ex_ty, 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(path);
let path_str = rustc_hir_pretty::qpath_to_string(&cx.tcx, path);
if path_str == "Err" {
let mut matching_wild = inner.iter().any(is_wild);
let mut ident_bind_name = kw::Underscore;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
cx,
arguments.iter().collect(),
cx.typeck_results().expr_ty(fn_expr),
&rustc_hir_pretty::qpath_to_string(path),
&rustc_hir_pretty::qpath_to_string(&cx.tcx, path),
"function",
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/value-suggestion-ice-123906.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/value-suggestion-ice-123906.rs:3:9
|
LL | fn as_chunks<const N: usize>() -> [u8; N] {
| ------- expected `[u8; ]` because of this return type
| ------- expected `[u8; N]` because of this return type
LL | loop {
| ---- this loop is expected to be of type `[u8; N]`
LL | break;
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/enum/error-variant-with-turbofishes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum Struct<const N: usize> { Variant { x: [(); N] } }

fn test() {
let x = Struct::<0>::Variant;
//~^ ERROR expected value, found struct variant `Struct<0>::Variant`
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/enum/error-variant-with-turbofishes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0533]: expected value, found struct variant `Struct<0>::Variant`
--> $DIR/error-variant-with-turbofishes.rs:4:13
|
LL | let x = Struct::<0>::Variant;
| ^^^^^^^^^^^^^^^^^^^^ not a value

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0533`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ fn main() {
//~^ ERROR expected a pattern, found an expression
//~| ERROR cannot find type `T` in this scope
//~| ERROR const and type arguments are not allowed on builtin type `str`
//~| ERROR expected unit struct, unit variant or constant, found associated function `str<, T>::as_bytes`
//~| ERROR expected unit struct, unit variant or constant, found associated function `str<
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ LL - let str::<{fn str() { let str::T>>::as_bytes; }}, T>::as_bytes;
LL + let str::as_bytes;
|

error[E0533]: expected unit struct, unit variant or constant, found associated function `str<, T>::as_bytes`
error[E0533]: expected unit struct, unit variant or constant, found associated function `str<{
fn str() { let (/*ERROR*/); }
}, T>::as_bytes`
--> $DIR/ensure-overriding-bindings-in-pattern-with-ty-err-doesnt-ice.rs:2:9
|
LL | let str::<{fn str() { let str::T>>::as_bytes; }}, T>::as_bytes;
Expand Down

0 comments on commit 56352ad

Please sign in to comment.