Skip to content

Commit 73b3ce2

Browse files
committed
improve diagnostics and bless tests
1 parent 442617c commit 73b3ce2

File tree

65 files changed

+644
-481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+644
-481
lines changed

compiler/rustc_resolve/messages.ftl

+27-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ resolve_param_in_ty_of_const_param =
145145
the type of const parameters must not depend on other generic parameters
146146
.label = the type must not depend on the parameter `{$name}`
147147
148+
resolve_type_param_in_ty_of_const_param =
149+
type parameters may not be used in the type of const parameters
150+
151+
resolve_const_param_in_ty_of_const_param =
152+
const parameters may not be used in the type of const parameters
153+
154+
resolve_lifetime_param_in_ty_of_const_param =
155+
lifetime parameters may not be used in the type of const parameters
156+
148157
resolve_self_in_generic_param_default =
149158
generic parameters cannot use `Self` in their defaults
150159
.label = `Self` in generic parameter default
@@ -156,12 +165,15 @@ resolve_param_in_non_trivial_anon_const =
156165
resolve_param_in_non_trivial_anon_const_help =
157166
use `#![feature(generic_const_exprs)]` to allow generic const expressions
158167
159-
resolve_param_in_non_trivial_anon_const_sub_type =
168+
resolve_type_param_in_non_trivial_anon_const =
160169
type parameters may not be used in const expressions
161170
162-
resolve_param_in_non_trivial_anon_const_sub_non_type =
171+
resolve_const_param_in_non_trivial_anon_const =
163172
const parameters may only be used as standalone arguments, i.e. `{$name}`
164173
174+
resolve_lifetime_param_in_non_trivial_anon_const =
175+
lifetime parameters may not be used in const expressions
176+
165177
resolve_unreachable_label =
166178
use of unreachable label `{$name}`
167179
.label = unreachable label `{$name}`
@@ -233,3 +245,16 @@ resolve_macro_use_extern_crate_self = `#[macro_use]` is not supported on `extern
233245
234246
resolve_accessible_unsure = not sure whether the path is accessible or not
235247
.note = the type may have associated items, but we are currently not checking them
248+
249+
resolve_param_in_enum_discriminant =
250+
generic parameters may not be used in enum discriminant values
251+
.label = cannot perform const operation using `{$name}`
252+
253+
resolve_type_param_in_enum_discriminant =
254+
type parameters may not be used in enum discriminant values
255+
256+
resolve_const_param_in_enum_discriminant =
257+
const parameters may not be used in enum discriminant values
258+
259+
resolve_lifetime_param_in_enum_discriminant =
260+
lifetime parameters may not be used in enum discriminant values

compiler/rustc_resolve/src/diagnostics.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -864,25 +864,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
864864
ResolutionError::ForwardDeclaredGenericParam => {
865865
self.tcx.sess.create_err(errs::ForwardDeclaredGenericParam { span })
866866
}
867-
ResolutionError::ParamInTyOfConstParam(name) => {
868-
self.tcx.sess.create_err(errs::ParamInTyOfConstParam { span, name })
869-
}
870-
ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
867+
ResolutionError::ParamInTyOfConstParam { name, param_kind: is_type } => self
868+
.tcx
869+
.sess
870+
.create_err(errs::ParamInTyOfConstParam { span, name, param_kind: is_type }),
871+
ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type } => {
871872
self.tcx.sess.create_err(errs::ParamInNonTrivialAnonConst {
872873
span,
873874
name,
874-
sub_is_type: if is_type {
875-
errs::ParamInNonTrivialAnonConstIsType::AType
876-
} else {
877-
errs::ParamInNonTrivialAnonConstIsType::NotAType { name }
878-
},
875+
param_kind: is_type,
879876
help: self
880877
.tcx
881878
.sess
882879
.is_nightly_build()
883880
.then_some(errs::ParamInNonTrivialAnonConstHelp),
884881
})
885882
}
883+
ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self
884+
.tcx
885+
.sess
886+
.create_err(errs::ParamInEnumDiscriminant { span, name, param_kind: is_type }),
886887
ResolutionError::SelfInGenericParamDefault => {
887888
self.tcx.sess.create_err(errs::SelfInGenericParamDefault { span })
888889
}

compiler/rustc_resolve/src/errors.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,18 @@ pub(crate) struct ParamInTyOfConstParam {
326326
#[label]
327327
pub(crate) span: Span,
328328
pub(crate) name: Symbol,
329+
#[subdiagnostic]
330+
pub(crate) param_kind: Option<ParamKindInTyOfConstParam>,
331+
}
332+
333+
#[derive(Subdiagnostic)]
334+
pub(crate) enum ParamKindInTyOfConstParam {
335+
#[note(resolve_type_param_in_ty_of_const_param)]
336+
Type,
337+
#[note(resolve_const_param_in_ty_of_const_param)]
338+
Const,
339+
#[note(resolve_lifetime_param_in_ty_of_const_param)]
340+
Lifetime,
329341
}
330342

331343
#[derive(Diagnostic)]
@@ -344,7 +356,7 @@ pub(crate) struct ParamInNonTrivialAnonConst {
344356
pub(crate) span: Span,
345357
pub(crate) name: Symbol,
346358
#[subdiagnostic]
347-
pub(crate) sub_is_type: ParamInNonTrivialAnonConstIsType,
359+
pub(crate) param_kind: ParamKindInNonTrivialAnonConst,
348360
#[subdiagnostic]
349361
pub(crate) help: Option<ParamInNonTrivialAnonConstHelp>,
350362
}
@@ -354,11 +366,13 @@ pub(crate) struct ParamInNonTrivialAnonConst {
354366
pub(crate) struct ParamInNonTrivialAnonConstHelp;
355367

356368
#[derive(Subdiagnostic)]
357-
pub(crate) enum ParamInNonTrivialAnonConstIsType {
358-
#[note(resolve_param_in_non_trivial_anon_const_sub_type)]
359-
AType,
360-
#[help(resolve_param_in_non_trivial_anon_const_sub_non_type)]
361-
NotAType { name: Symbol },
369+
pub(crate) enum ParamKindInNonTrivialAnonConst {
370+
#[note(resolve_type_param_in_non_trivial_anon_const)]
371+
Type,
372+
#[help(resolve_const_param_in_non_trivial_anon_const)]
373+
Const { name: Symbol },
374+
#[note(resolve_lifetime_param_in_non_trivial_anon_const)]
375+
Lifetime,
362376
}
363377

364378
#[derive(Diagnostic)]
@@ -539,3 +553,24 @@ pub(crate) struct CfgAccessibleUnsure {
539553
#[primary_span]
540554
pub(crate) span: Span,
541555
}
556+
557+
#[derive(Diagnostic)]
558+
#[diag(resolve_param_in_enum_discriminant)]
559+
pub(crate) struct ParamInEnumDiscriminant {
560+
#[primary_span]
561+
#[label]
562+
pub(crate) span: Span,
563+
pub(crate) name: Symbol,
564+
#[subdiagnostic]
565+
pub(crate) param_kind: ParamKindInEnumDiscriminant,
566+
}
567+
568+
#[derive(Subdiagnostic)]
569+
pub(crate) enum ParamKindInEnumDiscriminant {
570+
#[note(resolve_type_param_in_enum_discriminant)]
571+
Type,
572+
#[note(resolve_const_param_in_enum_discriminant)]
573+
Const,
574+
#[note(resolve_lifetime_param_in_enum_discriminant)]
575+
Lifetime,
576+
}

compiler/rustc_resolve/src/ident.rs

+51-19
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use rustc_span::{Span, DUMMY_SP};
1313

1414
use std::ptr;
1515

16+
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
1617
use crate::late::{
17-
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
18+
ConstantHasGenerics, ConstantItemKind, HasGenericParams, NoConstantGenericsReason, PathSource,
19+
Rib, RibKind,
1820
};
1921
use crate::macros::{sub_namespace_match, MacroRulesScope};
2022
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
@@ -1153,7 +1155,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11531155
}
11541156
RibKind::ConstParamTy => {
11551157
if let Some(span) = finalize {
1156-
self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
1158+
self.report_error(
1159+
span,
1160+
ParamInTyOfConstParam {
1161+
name: rib_ident.name,
1162+
param_kind: None,
1163+
},
1164+
);
11571165
}
11581166
return Res::Err;
11591167
}
@@ -1206,13 +1214,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12061214
}
12071215
} else {
12081216
if let Some(span) = finalize {
1209-
self.report_error(
1210-
span,
1211-
ResolutionError::ParamInNonTrivialAnonConst {
1212-
name: rib_ident.name,
1213-
is_type: true,
1214-
},
1215-
);
1217+
let error = match cause {
1218+
NoConstantGenericsReason::IsEnumDiscriminant => {
1219+
ResolutionError::ParamInEnumDiscriminant {
1220+
name: rib_ident.name,
1221+
param_kind: ParamKindInEnumDiscriminant::Type,
1222+
}
1223+
}
1224+
NoConstantGenericsReason::NonTrivialConstArg => {
1225+
ResolutionError::ParamInNonTrivialAnonConst {
1226+
name: rib_ident.name,
1227+
param_kind:
1228+
ParamKindInNonTrivialAnonConst::Type,
1229+
}
1230+
}
1231+
};
1232+
self.report_error(span, error);
12161233
self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
12171234
}
12181235

@@ -1229,7 +1246,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12291246
if let Some(span) = finalize {
12301247
self.report_error(
12311248
span,
1232-
ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1249+
ResolutionError::ParamInTyOfConstParam {
1250+
name: rib_ident.name,
1251+
param_kind: Some(errors::ParamKindInTyOfConstParam::Type),
1252+
},
12331253
);
12341254
}
12351255
return Res::Err;
@@ -1262,14 +1282,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12621282
RibKind::ConstantItem(trivial, _) => {
12631283
if let ConstantHasGenerics::No(cause) = trivial {
12641284
if let Some(span) = finalize {
1265-
self.report_error(
1266-
span,
1267-
ResolutionError::ParamInNonTrivialAnonConst {
1268-
name: rib_ident.name,
1269-
is_type: false,
1270-
},
1271-
);
1272-
self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
1285+
let error = match cause {
1286+
NoConstantGenericsReason::IsEnumDiscriminant => {
1287+
ResolutionError::ParamInEnumDiscriminant {
1288+
name: rib_ident.name,
1289+
param_kind: ParamKindInEnumDiscriminant::Const,
1290+
}
1291+
}
1292+
NoConstantGenericsReason::NonTrivialConstArg => {
1293+
ResolutionError::ParamInNonTrivialAnonConst {
1294+
name: rib_ident.name,
1295+
param_kind: ParamKindInNonTrivialAnonConst::Const {
1296+
name: rib_ident.name,
1297+
},
1298+
}
1299+
}
1300+
};
1301+
self.report_error(span, error);
12731302
}
12741303

12751304
return Res::Err;
@@ -1283,7 +1312,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12831312
if let Some(span) = finalize {
12841313
self.report_error(
12851314
span,
1286-
ResolutionError::ParamInTyOfConstParam(rib_ident.name),
1315+
ResolutionError::ParamInTyOfConstParam {
1316+
name: rib_ident.name,
1317+
param_kind: Some(errors::ParamKindInTyOfConstParam::Const),
1318+
},
12871319
);
12881320
}
12891321
return Res::Err;

compiler/rustc_resolve/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15291529
match rib.kind {
15301530
LifetimeRibKind::Item => break,
15311531
LifetimeRibKind::ConstParamTy => {
1532-
self.emit_non_static_lt_in_const_generic_error(lifetime);
1532+
self.emit_non_static_lt_in_const_param_ty_error(lifetime);
15331533
self.record_lifetime_res(
15341534
lifetime.id,
15351535
LifetimeRes::Error,
@@ -1538,7 +1538,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15381538
return;
15391539
}
15401540
LifetimeRibKind::ConcreteAnonConst(cause) => {
1541-
self.maybe_emit_forbidden_non_static_lifetime_error(lifetime);
1541+
self.emit_forbidden_non_static_lifetime_error(cause, lifetime);
15421542
self.record_lifetime_res(
15431543
lifetime.id,
15441544
LifetimeRes::Error,

compiler/rustc_resolve/src/late/diagnostics.rs

+45-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
22
use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind};
33
use crate::late::{LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseSet};
4-
use crate::path_names_to_string;
4+
use crate::{errors, path_names_to_string};
55
use crate::{Module, ModuleKind, ModuleOrUniformRoot};
66
use crate::{PathResult, PathSource, Segment};
77

@@ -22,7 +22,6 @@ use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
2222
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
2323
use rustc_hir::PrimTy;
2424
use rustc_session::lint;
25-
use rustc_session::parse::feature_err;
2625
use rustc_session::Session;
2726
use rustc_span::edit_distance::find_best_match_for_name;
2827
use rustc_span::edition::Edition;
@@ -35,6 +34,8 @@ use std::ops::Deref;
3534

3635
use thin_vec::ThinVec;
3736

37+
use super::NoConstantGenericsReason;
38+
3839
type Res = def::Res<ast::NodeId>;
3940

4041
/// A field or associated item from self type suggested in case of resolution failure.
@@ -2316,37 +2317,56 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
23162317
}
23172318
}
23182319

2319-
pub(crate) fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &ast::Lifetime) {
2320-
struct_span_err!(
2321-
self.r.tcx.sess,
2322-
lifetime_ref.ident.span,
2323-
E0771,
2324-
"use of non-static lifetime `{}` in const generic",
2325-
lifetime_ref.ident
2326-
)
2327-
.note(
2328-
"for more information, see issue #74052 \
2329-
<https://github.com/rust-lang/rust/issues/74052>",
2330-
)
2331-
.emit();
2320+
pub(crate) fn emit_non_static_lt_in_const_param_ty_error(&self, lifetime_ref: &ast::Lifetime) {
2321+
self.r
2322+
.tcx
2323+
.sess
2324+
.create_err(errors::ParamInTyOfConstParam {
2325+
span: lifetime_ref.ident.span,
2326+
name: lifetime_ref.ident.name,
2327+
param_kind: Some(errors::ParamKindInTyOfConstParam::Lifetime),
2328+
})
2329+
.emit();
23322330
}
23332331

23342332
/// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
23352333
/// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
23362334
/// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
2337-
pub(crate) fn maybe_emit_forbidden_non_static_lifetime_error(
2335+
pub(crate) fn emit_forbidden_non_static_lifetime_error(
23382336
&self,
2337+
cause: NoConstantGenericsReason,
23392338
lifetime_ref: &ast::Lifetime,
23402339
) {
2341-
let feature_active = self.r.tcx.sess.features_untracked().generic_const_exprs;
2342-
if !feature_active {
2343-
feature_err(
2344-
&self.r.tcx.sess.parse_sess,
2345-
sym::generic_const_exprs,
2346-
lifetime_ref.ident.span,
2347-
"a non-static lifetime is not allowed in a `const`",
2348-
)
2349-
.emit();
2340+
match cause {
2341+
NoConstantGenericsReason::IsEnumDiscriminant => {
2342+
self.r
2343+
.tcx
2344+
.sess
2345+
.create_err(errors::ParamInEnumDiscriminant {
2346+
span: lifetime_ref.ident.span,
2347+
name: lifetime_ref.ident.name,
2348+
param_kind: errors::ParamKindInEnumDiscriminant::Lifetime,
2349+
})
2350+
.emit();
2351+
}
2352+
NoConstantGenericsReason::NonTrivialConstArg => {
2353+
assert!(!self.r.tcx.features().generic_const_exprs);
2354+
self.r
2355+
.tcx
2356+
.sess
2357+
.create_err(errors::ParamInNonTrivialAnonConst {
2358+
span: lifetime_ref.ident.span,
2359+
name: lifetime_ref.ident.name,
2360+
param_kind: errors::ParamKindInNonTrivialAnonConst::Lifetime,
2361+
help: self
2362+
.r
2363+
.tcx
2364+
.sess
2365+
.is_nightly_build()
2366+
.then_some(errors::ParamInNonTrivialAnonConstHelp),
2367+
})
2368+
.emit();
2369+
}
23502370
}
23512371
}
23522372

0 commit comments

Comments
 (0)