-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Allow using named consts in pattern types #136284
Changes from 1 commit
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 |
---|---|---|
|
@@ -18,9 +18,10 @@ use crate::hir_ty_lowering::HirTyLowerer; | |
|
||
mod opaque; | ||
|
||
fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { | ||
fn anon_const_type_of<'tcx>(icx: &ItemCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { | ||
use hir::*; | ||
use rustc_middle::ty::Ty; | ||
let tcx = icx.tcx; | ||
let hir_id = tcx.local_def_id_to_hir_id(def_id); | ||
|
||
let node = tcx.hir_node(hir_id); | ||
|
@@ -54,7 +55,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { | |
hir_id: arg_hir_id, | ||
kind: ConstArgKind::Anon(&AnonConst { hir_id: anon_hir_id, .. }), | ||
.. | ||
}) if anon_hir_id == hir_id => const_arg_anon_type_of(tcx, arg_hir_id, span), | ||
}) if anon_hir_id == hir_id => const_arg_anon_type_of(icx, arg_hir_id, span), | ||
|
||
// Anon consts outside the type system. | ||
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. }) | ||
|
@@ -138,18 +139,28 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { | |
} | ||
} | ||
|
||
fn const_arg_anon_type_of<'tcx>(tcx: TyCtxt<'tcx>, arg_hir_id: HirId, span: Span) -> Ty<'tcx> { | ||
fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: Span) -> Ty<'tcx> { | ||
use hir::*; | ||
use rustc_middle::ty::Ty; | ||
|
||
let tcx = icx.tcx; | ||
|
||
match tcx.parent_hir_node(arg_hir_id) { | ||
// Array length const arguments do not have `type_of` fed as there is never a corresponding | ||
// generic parameter definition. | ||
Node::Ty(&hir::Ty { kind: TyKind::Array(_, ref constant), .. }) | ||
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) | ||
if constant.hir_id == arg_hir_id => | ||
{ | ||
return tcx.types.usize; | ||
tcx.types.usize | ||
} | ||
|
||
Node::TyPat(pat) => { | ||
let hir::TyKind::Pat(ty, p) = tcx.parent_hir_node(pat.hir_id).expect_ty().kind else { | ||
bug!() | ||
}; | ||
assert_eq!(p.hir_id, pat.hir_id); | ||
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. why do we use the same 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. We don't. This assertion is just 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. 👍 |
||
icx.lower_ty(ty) | ||
} | ||
|
||
// This is not a `bug!` as const arguments in path segments that did not resolve to anything | ||
|
@@ -344,7 +355,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_ | |
tcx.typeck(def_id).node_type(hir_id) | ||
} | ||
|
||
Node::AnonConst(_) => anon_const_type_of(tcx, def_id), | ||
Node::AnonConst(_) => anon_const_type_of(&icx, def_id), | ||
|
||
Node::ConstBlock(_) => { | ||
let args = ty::GenericArgs::identity_for_item(tcx, def_id.to_def_id()); | ||
|
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.
this is pretty much mgce? 🤔 as in, this also adds consts in the type system.
Can you delegate this to lower_const_arg or whatever instead?
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.
Yes, but at that point I should modify the ast, and I'd prefer to do that in other PRs
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.
please add a FIXME then 😁