Skip to content

Commit 94de249

Browse files
committed
Auto merge of #152913 - Unique-Usman:ua/constnottype, r=<try>
rustc_resolve: improve const generic errors
2 parents d1ee5e5 + aa46052 commit 94de249

20 files changed

+526
-314
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
};
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::DefId;
9-
use rustc_hir::{self as hir, GenericArg};
9+
use rustc_hir::{self as hir, GenericArg, QPath, TyKind};
1010
use rustc_middle::ty::{
1111
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty,
1212
};
@@ -44,6 +44,16 @@ fn generic_arg_mismatch_err(
4444
param.kind.descr(),
4545
);
4646

47+
if let GenericArg::Type(ty, ..) = arg
48+
&& let TyKind::Path(qpath) = &ty.kind
49+
&& let QPath::Resolved(_, path) = qpath
50+
{
51+
let res = path.res;
52+
if matches!(res, Res::Err) {
53+
return err.delay_as_bug();
54+
}
55+
}
56+
4757
let add_braces_suggestion = |arg: &GenericArg<'_>, err: &mut Diag<'_>| {
4858
let suggestions = vec![
4959
(arg.span().shrink_to_lo(), String::from("{ ")),

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
898898
}
899899

900900
// These items live in both the type and value namespaces.
901-
ItemKind::Struct(ident, _, ref vdata) => {
901+
ItemKind::Struct(ident, ref generics, ref vdata) => {
902902
self.build_reduced_graph_for_struct_variant(
903903
vdata.fields(),
904904
ident,
@@ -947,6 +947,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
947947
.struct_constructors
948948
.insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
949949
}
950+
self.r.struct_generics.insert(local_def_id, generics.clone());
950951
}
951952

952953
ItemKind::Union(ident, _, ref vdata) => {

compiler/rustc_resolve/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,20 @@ pub(crate) struct UnexpectedResChangeTyToConstParamSugg {
883883
pub applicability: Applicability,
884884
}
885885

886+
#[derive(Subdiagnostic)]
887+
#[suggestion(
888+
"you might have meant to introduce a const parameter on the `{$item_name}`",
889+
code = "{snippet}",
890+
applicability = "machine-applicable",
891+
style = "verbose"
892+
)]
893+
pub(crate) struct UnexpectedMissingConstParameter {
894+
#[primary_span]
895+
pub span: Span,
896+
pub snippet: String,
897+
pub item_name: String,
898+
}
899+
886900
#[derive(Subdiagnostic)]
887901
#[multipart_suggestion(
888902
"you might have meant to write a const parameter here",

compiler/rustc_resolve/src/late.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4425,7 +4425,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44254425
let Finalize { node_id, path_span, .. } = finalize;
44264426
let report_errors = |this: &mut Self, res: Option<Res>| {
44274427
if this.should_report_errs() {
4428-
let (err, candidates) = this.smart_resolve_report_errors(
4428+
let (mut err, candidates) = this.smart_resolve_report_errors(
44294429
path,
44304430
None,
44314431
path_span,
@@ -4436,7 +4436,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44364436

44374437
let def_id = this.parent_scope.module.nearest_parent_mod();
44384438
let instead = res.is_some();
4439-
let suggestion = if let Some((start, end)) = this.diag_metadata.in_range
4439+
let (suggestion, const_err) = if let Some((start, end)) =
4440+
this.diag_metadata.in_range
44404441
&& path[0].ident.span.lo() == end.span.lo()
44414442
&& !matches!(start.kind, ExprKind::Lit(_))
44424443
{
@@ -4448,22 +4449,30 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44484449
span = span.with_lo(span.lo() + BytePos(1));
44494450
sugg = "";
44504451
}
4451-
Some((
4452-
span,
4453-
"you might have meant to write `.` instead of `..`",
4454-
sugg.to_string(),
4455-
Applicability::MaybeIncorrect,
4456-
))
4452+
(
4453+
Some((
4454+
span,
4455+
"you might have meant to write `.` instead of `..`",
4456+
sugg.to_string(),
4457+
Applicability::MaybeIncorrect,
4458+
)),
4459+
None,
4460+
)
44574461
} else if res.is_none()
44584462
&& let PathSource::Type
44594463
| PathSource::Expr(_)
44604464
| PathSource::PreciseCapturingArg(..) = source
44614465
{
44624466
this.suggest_adding_generic_parameter(path, source)
44634467
} else {
4464-
None
4468+
(None, None)
44654469
};
44664470

4471+
if let Some(const_err) = const_err {
4472+
err.cancel();
4473+
err = const_err;
4474+
}
4475+
44674476
let ue = UseError {
44684477
err,
44694478
candidates,

0 commit comments

Comments
 (0)