X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_hir_typeck%2Fsrc%2Flib.rs;h=5104b448023236628cbd331d9ef885033017b8b9;hb=a673364c543986789cfbb844c925063519fb872a;hp=334d6d0aa6c209838c6a042895e8d94eab3e7742;hpb=379d3365fd16a9faf90c98a74f5e7d7b5c0ede23;p=rust.git diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 334d6d0aa6c..5104b448023 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -53,9 +53,9 @@ use crate::coercion::DynamicCoerceMany; use crate::gather_locals::GatherLocalsVisitor; use rustc_data_structures::unord::UnordSet; -use rustc_errors::{struct_span_err, ErrorGuaranteed, MultiSpan}; +use rustc_errors::{struct_span_err, DiagnosticId, ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; -use rustc_hir::def::Res; +use rustc_hir::def::{DefKind, Res}; use rustc_hir::intravisit::Visitor; use rustc_hir::{HirIdMap, Node}; use rustc_hir_analysis::astconv::AstConv; @@ -433,15 +433,27 @@ fn report_unexpected_variant_res( res: Res, qpath: &hir::QPath<'_>, span: Span, + err_code: &str, + expected: &str, ) -> ErrorGuaranteed { - struct_span_err!( - tcx.sess, + let res_descr = match res { + Res::Def(DefKind::Variant, _) => "struct variant", + _ => res.descr(), + }; + let path_str = rustc_hir_pretty::qpath_to_string(qpath); + let mut err = tcx.sess.struct_span_err_with_code( span, - E0533, - "expected unit struct, unit variant or constant, found {} `{}`", - res.descr(), - rustc_hir_pretty::qpath_to_string(qpath), - ) + format!("expected {expected}, found {res_descr} `{path_str}`"), + DiagnosticId::Error(err_code.into()), + ); + match res { + Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => { + let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html"; + err.span_label(span, "`fn` calls are not allowed in patterns"); + err.help(format!("for more information, visit {patterns_url}")) + } + _ => err.span_label(span, format!("not a {expected}")), + } .emit() }