Skip to content

Commit f60b649

Browse files
committed
rustc_resolve: improve const generic errors
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
1 parent fd0c901 commit f60b649

File tree

12 files changed

+464
-133
lines changed

12 files changed

+464
-133
lines changed

‎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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,21 @@ 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 `{$item_name}` on the {$item_location}",
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+
pub item_location: String,
899+
}
900+
886901
#[derive(Subdiagnostic)]
887902
#[multipart_suggestion(
888903
"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)