Misc fixes (pattern type lowering, cfi, pretty printing)

This commit is contained in:
Boxy 2024-06-03 03:38:09 +01:00
parent a454da3b1c
commit 56092a345b
4 changed files with 36 additions and 40 deletions

View file

@ -2160,17 +2160,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
} }
_ => (expr, None), _ => (expr, None),
}; };
let c = match &expr.kind { let (c, c_ty) = match &expr.kind {
hir::ExprKind::Lit(lit) => { hir::ExprKind::Lit(lit) => {
let lit_input = let lit_input =
LitToConstInput { lit: &lit.node, ty, neg: neg.is_some() }; LitToConstInput { lit: &lit.node, ty, neg: neg.is_some() };
match tcx.lit_to_const(lit_input) { let ct = match tcx.lit_to_const(lit_input) {
Ok(c) => c, Ok(c) => c,
Err(LitToConstError::Reported(err)) => { Err(LitToConstError::Reported(err)) => {
ty::Const::new_error(tcx, err) ty::Const::new_error(tcx, err)
} }
Err(LitToConstError::TypeError) => todo!(), Err(LitToConstError::TypeError) => todo!(),
} };
(ct, ty)
} }
hir::ExprKind::Path(hir::QPath::Resolved( hir::ExprKind::Path(hir::QPath::Resolved(
@ -2188,20 +2189,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.type_of(def_id) .type_of(def_id)
.no_bound_vars() .no_bound_vars()
.expect("const parameter types cannot be generic"); .expect("const parameter types cannot be generic");
self.lower_const_param(expr.hir_id) let ct = self.lower_const_param(expr.hir_id);
(ct, ty)
} }
_ => { _ => {
let err = tcx let err = tcx
.dcx() .dcx()
.emit_err(crate::errors::NonConstRange { span: expr.span }); .emit_err(crate::errors::NonConstRange { span: expr.span });
ty::Const::new_error(tcx, err) (ty::Const::new_error(tcx, err), Ty::new_error(tcx, err))
} }
}; };
// THISPR self.record_ty(expr.hir_id, c_ty, expr.span);
self.record_ty(expr.hir_id, todo!(), expr.span);
if let Some((id, span)) = neg { if let Some((id, span)) = neg {
self.record_ty(id, todo!(), span); self.record_ty(id, c_ty, span);
} }
c c
}; };

View file

@ -143,10 +143,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
// `trait_object_dummy_self`, so check for that. // `trait_object_dummy_self`, so check for that.
let references_self = match pred.skip_binder().term.unpack() { let references_self = match pred.skip_binder().term.unpack() {
ty::TermKind::Ty(ty) => ty.walk().any(|arg| arg == dummy_self.into()), ty::TermKind::Ty(ty) => ty.walk().any(|arg| arg == dummy_self.into()),
ty::TermKind::Const(c) => { // FIXME(associated_const_equality): We should walk the const instead of not doing anything
// THISPR ty::TermKind::Const(_) => false,
false
}
}; };
// If the projection output contains `Self`, force the user to // If the projection output contains `Self`, force the user to

View file

@ -1459,23 +1459,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(()); return Ok(());
} }
macro_rules! print_underscore {
() => {{
if print_ty {
self.typed_value(
|this| {
write!(this, "_")?;
Ok(())
},
|this| this.print_type(todo!()),
": ",
)?;
} else {
write!(self, "_")?;
}
}};
}
match ct.kind() { match ct.kind() {
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => { ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => {
match self.tcx().def_kind(def) { match self.tcx().def_kind(def) {
@ -1508,7 +1491,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
ty::InferConst::Var(ct_vid) if let Some(name) = self.const_infer_name(ct_vid) => { ty::InferConst::Var(ct_vid) if let Some(name) = self.const_infer_name(ct_vid) => {
p!(write("{}", name)) p!(write("{}", name))
} }
_ => print_underscore!(), _ => write!(self, "_")?,
}, },
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)), ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
ty::ConstKind::Value(ty, value) => { ty::ConstKind::Value(ty, value) => {

View file

@ -68,6 +68,8 @@ fn compress<'tcx>(
fn encode_args<'tcx>( fn encode_args<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
args: GenericArgsRef<'tcx>, args: GenericArgsRef<'tcx>,
for_def: DefId,
has_erased_self: bool,
dict: &mut FxHashMap<DictKey<'tcx>, usize>, dict: &mut FxHashMap<DictKey<'tcx>, usize>,
options: EncodeTyOptions, options: EncodeTyOptions,
) -> String { ) -> String {
@ -76,7 +78,8 @@ fn encode_args<'tcx>(
let args: Vec<GenericArg<'_>> = args.iter().collect(); let args: Vec<GenericArg<'_>> = args.iter().collect();
if !args.is_empty() { if !args.is_empty() {
s.push('I'); s.push('I');
for arg in args { let def_generics = tcx.generics_of(for_def);
for (n, arg) in args.iter().enumerate() {
match arg.unpack() { match arg.unpack() {
GenericArgKind::Lifetime(region) => { GenericArgKind::Lifetime(region) => {
s.push_str(&encode_region(region, dict)); s.push_str(&encode_region(region, dict));
@ -85,7 +88,10 @@ fn encode_args<'tcx>(
s.push_str(&encode_ty(tcx, ty, dict, options)); s.push_str(&encode_ty(tcx, ty, dict, options));
} }
GenericArgKind::Const(c) => { GenericArgKind::Const(c) => {
s.push_str(&encode_const(tcx, c, dict, options)); let n = n + (has_erased_self as usize);
let ct_ty =
tcx.type_of(def_generics.param_at(n, tcx).def_id).instantiate_identity();
s.push_str(&encode_const(tcx, c, ct_ty, dict, options));
} }
} }
} }
@ -99,6 +105,7 @@ fn encode_args<'tcx>(
fn encode_const<'tcx>( fn encode_const<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
c: Const<'tcx>, c: Const<'tcx>,
ct_ty: Ty<'tcx>,
dict: &mut FxHashMap<DictKey<'tcx>, usize>, dict: &mut FxHashMap<DictKey<'tcx>, usize>,
options: EncodeTyOptions, options: EncodeTyOptions,
) -> String { ) -> String {
@ -111,8 +118,7 @@ fn encode_const<'tcx>(
// L<element-type>E as literal argument // L<element-type>E as literal argument
// Element type // Element type
// THISPR s.push_str(&encode_ty(tcx, ct_ty, dict, options));
s.push_str(&encode_ty(tcx, todo!(), dict, options));
} }
// Literal arguments // Literal arguments
@ -232,15 +238,21 @@ fn encode_predicate<'tcx>(
ty::ExistentialPredicate::Trait(trait_ref) => { ty::ExistentialPredicate::Trait(trait_ref) => {
let name = encode_ty_name(tcx, trait_ref.def_id); let name = encode_ty_name(tcx, trait_ref.def_id);
let _ = write!(s, "u{}{}", name.len(), &name); let _ = write!(s, "u{}{}", name.len(), &name);
s.push_str(&encode_args(tcx, trait_ref.args, dict, options)); s.push_str(&encode_args(tcx, trait_ref.args, trait_ref.def_id, true, dict, options));
} }
ty::ExistentialPredicate::Projection(projection) => { ty::ExistentialPredicate::Projection(projection) => {
let name = encode_ty_name(tcx, projection.def_id); let name = encode_ty_name(tcx, projection.def_id);
let _ = write!(s, "u{}{}", name.len(), &name); let _ = write!(s, "u{}{}", name.len(), &name);
s.push_str(&encode_args(tcx, projection.args, dict, options)); s.push_str(&encode_args(tcx, projection.args, projection.def_id, true, dict, options));
match projection.term.unpack() { match projection.term.unpack() {
TermKind::Ty(ty) => s.push_str(&encode_ty(tcx, ty, dict, options)), TermKind::Ty(ty) => s.push_str(&encode_ty(tcx, ty, dict, options)),
TermKind::Const(c) => s.push_str(&encode_const(tcx, c, dict, options)), TermKind::Const(c) => s.push_str(&encode_const(
tcx,
c,
tcx.type_of(projection.def_id).instantiate(tcx, projection.args),
dict,
options,
)),
} }
} }
ty::ExistentialPredicate::AutoTrait(def_id) => { ty::ExistentialPredicate::AutoTrait(def_id) => {
@ -486,7 +498,7 @@ pub fn encode_ty<'tcx>(
// <subst>, as vendor extended type. // <subst>, as vendor extended type.
let name = encode_ty_name(tcx, def_id); let name = encode_ty_name(tcx, def_id);
let _ = write!(s, "u{}{}", name.len(), &name); let _ = write!(s, "u{}{}", name.len(), &name);
s.push_str(&encode_args(tcx, args, dict, options)); s.push_str(&encode_args(tcx, args, def_id, false, dict, options));
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s); compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
} }
typeid.push_str(&s); typeid.push_str(&s);
@ -530,7 +542,7 @@ pub fn encode_ty<'tcx>(
let mut s = String::new(); let mut s = String::new();
let name = encode_ty_name(tcx, *def_id); let name = encode_ty_name(tcx, *def_id);
let _ = write!(s, "u{}{}", name.len(), &name); let _ = write!(s, "u{}{}", name.len(), &name);
s.push_str(&encode_args(tcx, args, dict, options)); s.push_str(&encode_args(tcx, args, *def_id, false, dict, options));
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s); compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
typeid.push_str(&s); typeid.push_str(&s);
} }
@ -542,7 +554,7 @@ pub fn encode_ty<'tcx>(
let name = encode_ty_name(tcx, *def_id); let name = encode_ty_name(tcx, *def_id);
let _ = write!(s, "u{}{}", name.len(), &name); let _ = write!(s, "u{}{}", name.len(), &name);
let parent_args = tcx.mk_args(args.as_coroutine_closure().parent_args()); let parent_args = tcx.mk_args(args.as_coroutine_closure().parent_args());
s.push_str(&encode_args(tcx, parent_args, dict, options)); s.push_str(&encode_args(tcx, parent_args, *def_id, false, dict, options));
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s); compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
typeid.push_str(&s); typeid.push_str(&s);
} }
@ -557,6 +569,8 @@ pub fn encode_ty<'tcx>(
s.push_str(&encode_args( s.push_str(&encode_args(
tcx, tcx,
tcx.mk_args(args.as_coroutine().parent_args()), tcx.mk_args(args.as_coroutine().parent_args()),
*def_id,
false,
dict, dict,
options, options,
)); ));