-
-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Support function args const generics #162727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0958897
ea838b3
a4ee6d4
342e0bf
bcc2a8c
9720b05
228acb1
74ba4f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -546,6 +546,8 @@ declare_features! ( | |
| (internal, freeze_impls, "1.78.0", Some(121675)), | ||
| /// Frontmatter `---` blocks for use by external tools. | ||
| (unstable, frontmatter, "1.88.0", Some(136889)), | ||
| /// Allow function args to be const generics | ||
| (incomplete, function_arg_const_generics, "CURRENT_RUSTC_VERSION", Some(163261)), | ||
| /// Allows defining gen blocks and `gen fn`. | ||
| (unstable, gen_blocks, "1.75.0", Some(117078)), | ||
| /// Allows using generics in more complex const expressions, based on definitional equality. | ||
|
|
@@ -859,6 +861,7 @@ pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] = &[ | |
|
|
||
| /// Some features require one or more other features to be enabled. | ||
| pub const DEPENDENT_FEATURES: &[(Symbol, &[Symbol])] = &[ | ||
| (sym::function_arg_const_generics, &[sym::min_generic_const_args]), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, this is not strictly necessary - it is possible to use direct args right now on stable, plain simple paths to generic parameters are direct args on stable. Maaaybe still want to include it though, probably as being an |
||
| (sym::generic_const_args, &[sym::min_generic_const_args]), | ||
| (sym::macroless_generic_const_args, &[sym::min_generic_const_args]), | ||
| (sym::macroless_const_item_generic_const_args, &[sym::min_generic_const_args]), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2385,6 +2385,41 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
| self.check_param_uses_if_mcg(ct, tcx.hir_span(path_hir_id), false) | ||
| } | ||
|
|
||
| pub fn lower_const_arg_expr(&self, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would strongly prefer this to not be duplicated with the |
||
| let tcx = self.tcx(); | ||
| match expr.kind { | ||
| hir::ExprKind::Lit(lit) => { | ||
| self.lower_const_arg_literal(&lit.node, false, ty, expr.span) | ||
| } | ||
| hir::ExprKind::Unary( | ||
| hir::UnOp::Neg, | ||
| hir::Expr { kind: hir::ExprKind::Lit(lit), .. }, | ||
| ) => self.lower_const_arg_literal(&lit.node, true, ty, expr.span), | ||
| hir::ExprKind::Path(hir::QPath::Resolved(_, hir::Path { res: Res::Local(_), .. })) => { | ||
| Const::new_error(tcx, self.dcx().span_err(expr.span, "Path error")) | ||
| } | ||
| hir::ExprKind::Path(hir::QPath::Resolved(maybe_qself, path)) => { | ||
| let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself)); | ||
| self.lower_resolved_const_path(opt_self_ty, path, expr.hir_id) | ||
| } | ||
| hir::ExprKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment)) => { | ||
| let self_ty = self.lower_ty(hir_self_ty); | ||
| self.lower_type_relative_const_path( | ||
| self_ty, | ||
| hir_self_ty, | ||
| segment, | ||
| expr.hir_id, | ||
| expr.span, | ||
| ) | ||
| .unwrap_or_else(|guard| Const::new_error(tcx, guard)) | ||
| } | ||
| _ => Const::new_error( | ||
| tcx, | ||
| self.dcx().span_err(expr.span, "Some other variant we dont support"), | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| /// Lower a [`hir::ConstArg`] to a (type-level) [`ty::Const`]. | ||
| #[instrument(skip(self), level = "debug")] | ||
| pub fn lower_const_arg(&self, const_arg: &hir::ConstArg<'_>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially a better name, but I think especially a doc comment, would go a long way here. It's rather opaque/confusing what this is right now.