Skip to content

Commit 5b33de3

Browse files
committedMar 24, 2021
Auto merge of #75384 - JulianKnodt:cg_def, r=varkor,lcnr
implement `feature(const_generics_defaults)` Implements const generics defaults `struct Example<const N: usize=3>`, as well as a query for getting the default of a given const-parameter's def id. There are some remaining FIXME's but they were specified as not blocking for merging this PR. This also puts the defaults behind the unstable feature gate `#![feature(const_generics_defaults)]`. ~~This currently creates a field which is always false on `GenericParamDefKind` for future use when consts are permitted to have defaults. I'm not sure if this is exactly what is best for adding default parameters, but I mimicked the style of type defaults, so hopefully this is ok.~~ r? `@lcnr`
2 parents db492ec + 33370fd commit 5b33de3

File tree

73 files changed

+517
-191
lines changed

Some content is hidden

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

73 files changed

+517
-191
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22902290
this.lower_ty(&ty, ImplTraitContext::disallowed())
22912291
});
22922292
let default = default.as_ref().map(|def| self.lower_anon_const(def));
2293-
22942293
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty, default })
22952294
}
22962295
};

‎compiler/rustc_ast_passes/src/ast_validation.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1150,20 +1150,23 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11501150
}
11511151

11521152
fn visit_generics(&mut self, generics: &'a Generics) {
1153-
let mut prev_ty_default = None;
1153+
let cg_defaults = self.session.features_untracked().const_generics_defaults;
1154+
1155+
let mut prev_param_default = None;
11541156
for param in &generics.params {
11551157
match param.kind {
11561158
GenericParamKind::Lifetime => (),
1157-
GenericParamKind::Type { default: Some(_), .. } => {
1158-
prev_ty_default = Some(param.ident.span);
1159+
GenericParamKind::Type { default: Some(_), .. }
1160+
| GenericParamKind::Const { default: Some(_), .. } => {
1161+
prev_param_default = Some(param.ident.span);
11591162
}
11601163
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1161-
if let Some(span) = prev_ty_default {
1164+
if let Some(span) = prev_param_default {
11621165
let mut err = self.err_handler().struct_span_err(
11631166
span,
1164-
"type parameters with a default must be trailing",
1167+
"generic parameters with a default must be trailing",
11651168
);
1166-
if matches!(param.kind, GenericParamKind::Const { .. }) {
1169+
if matches!(param.kind, GenericParamKind::Const { .. }) && !cg_defaults {
11671170
err.note(
11681171
"using type defaults and const parameters \
11691172
in the same parameter list is currently not permitted",

0 commit comments

Comments
 (0)
Please sign in to comment.