|
| 1 | +use crate::clauses::ClauseBuilder; |
| 2 | +use crate::rust_ir::WellKnownTrait; |
| 3 | +use crate::{Interner, RustIrDatabase, TraitRef}; |
| 4 | +use chalk_ir::{ |
| 5 | + AliasTy, Floundered, Normalize, ProjectionTy, Substitution, Ty, TyKind, TyVariableKind, |
| 6 | +}; |
| 7 | + |
| 8 | +use super::last_field_of_struct; |
| 9 | + |
| 10 | +fn push_clauses<I: Interner>( |
| 11 | + db: &dyn RustIrDatabase<I>, |
| 12 | + builder: &mut ClauseBuilder<'_, I>, |
| 13 | + self_ty: Ty<I>, |
| 14 | + metadata: Ty<I>, |
| 15 | +) { |
| 16 | + let interner = db.interner(); |
| 17 | + let trait_id = db.well_known_trait_id(WellKnownTrait::Pointee).unwrap(); |
| 18 | + let substitution = Substitution::from1(interner, self_ty); |
| 19 | + let trait_datum = db.trait_datum(trait_id); |
| 20 | + assert_eq!( |
| 21 | + trait_datum.associated_ty_ids.len(), |
| 22 | + 1, |
| 23 | + "Pointee trait should have exactly one associated type, found {:?}", |
| 24 | + trait_datum.associated_ty_ids |
| 25 | + ); |
| 26 | + let metadata_id = trait_datum.associated_ty_ids[0]; |
| 27 | + let alias = AliasTy::Projection(ProjectionTy { |
| 28 | + associated_ty_id: metadata_id, |
| 29 | + substitution, |
| 30 | + }); |
| 31 | + builder.push_fact(Normalize { |
| 32 | + alias, |
| 33 | + ty: metadata, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +/// Add implicit impl for the `Pointee` trait for all types |
| 38 | +pub fn add_pointee_program_clauses<I: Interner>( |
| 39 | + db: &dyn RustIrDatabase<I>, |
| 40 | + builder: &mut ClauseBuilder<'_, I>, |
| 41 | + self_ty: Ty<I>, |
| 42 | +) -> Result<(), Floundered> { |
| 43 | + let interner = db.interner(); |
| 44 | + let trait_id = db.well_known_trait_id(WellKnownTrait::Pointee).unwrap(); |
| 45 | + let substitution = Substitution::from1(interner, self_ty.clone()); |
| 46 | + builder.push_fact(TraitRef { |
| 47 | + trait_id, |
| 48 | + substitution: substitution.clone(), |
| 49 | + }); |
| 50 | + match self_ty.kind(interner) { |
| 51 | + TyKind::Str | TyKind::Slice(_) => push_clauses( |
| 52 | + db, |
| 53 | + builder, |
| 54 | + self_ty.clone(), |
| 55 | + TyKind::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::Usize)).intern(interner), |
| 56 | + ), |
| 57 | + TyKind::Array(_, _) |
| 58 | + | TyKind::Never |
| 59 | + | TyKind::Closure(_, _) |
| 60 | + | TyKind::FnDef(_, _) |
| 61 | + | TyKind::Scalar(_) |
| 62 | + | TyKind::Raw(_, _) |
| 63 | + | TyKind::Function(_) |
| 64 | + | TyKind::InferenceVar(_, TyVariableKind::Float) |
| 65 | + | TyKind::InferenceVar(_, TyVariableKind::Integer) |
| 66 | + | TyKind::Generator(_, _) |
| 67 | + | TyKind::GeneratorWitness(_, _) |
| 68 | + | TyKind::Ref(_, _, _) => push_clauses( |
| 69 | + db, |
| 70 | + builder, |
| 71 | + self_ty, |
| 72 | + TyKind::Tuple(0, Substitution::empty(interner)).intern(interner), |
| 73 | + ), |
| 74 | + TyKind::Adt(id, subst) => { |
| 75 | + if let Some(last_field_ty) = last_field_of_struct(db, *id, subst) { |
| 76 | + push_for_last_field(last_field_ty, db, builder, self_ty); |
| 77 | + } else { |
| 78 | + push_clauses( |
| 79 | + db, |
| 80 | + builder, |
| 81 | + self_ty, |
| 82 | + TyKind::Tuple(0, Substitution::empty(interner)).intern(interner), |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + TyKind::Tuple(_, subst) => { |
| 87 | + let last_field_ty = subst |
| 88 | + .iter(interner) |
| 89 | + .rev() |
| 90 | + .next() |
| 91 | + .and_then(|x| x.ty(interner)) |
| 92 | + .cloned(); |
| 93 | + if let Some(last_field_ty) = last_field_ty { |
| 94 | + push_for_last_field(last_field_ty, db, builder, self_ty); |
| 95 | + } else { |
| 96 | + push_clauses( |
| 97 | + db, |
| 98 | + builder, |
| 99 | + self_ty, |
| 100 | + TyKind::Tuple(0, Substitution::empty(interner)).intern(interner), |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + TyKind::BoundVar(_) |
| 105 | + | TyKind::AssociatedType(_, _) |
| 106 | + | TyKind::OpaqueType(_, _) |
| 107 | + | TyKind::Foreign(_) |
| 108 | + | TyKind::Error |
| 109 | + | TyKind::Placeholder(_) |
| 110 | + | TyKind::Alias(_) => (), |
| 111 | + TyKind::Dyn(_) => { |
| 112 | + // FIXME: We should add a `Normalize(<dyn Trait as Pointee>::Metadata -> DynMetadata<dyn Trait>)` here, but |
| 113 | + // since chalk doesn't have the concept of lang item structs yet, we can't. |
| 114 | + } |
| 115 | + TyKind::InferenceVar(_, TyVariableKind::General) => return Err(Floundered), |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | + |
| 120 | +fn push_for_last_field<I: Interner>( |
| 121 | + last_field_ty: Ty<I>, |
| 122 | + db: &dyn RustIrDatabase<I>, |
| 123 | + builder: &mut ClauseBuilder<'_, I>, |
| 124 | + self_ty: Ty<I>, |
| 125 | +) { |
| 126 | + let interner = db.interner(); |
| 127 | + let _ = add_pointee_program_clauses(db, builder, last_field_ty.clone()); |
| 128 | + let trait_id = db.well_known_trait_id(WellKnownTrait::Pointee).unwrap(); |
| 129 | + let trait_datum = db.trait_datum(trait_id); |
| 130 | + assert_eq!( |
| 131 | + trait_datum.associated_ty_ids.len(), |
| 132 | + 1, |
| 133 | + "Pointee trait should have exactly one associated type, found {:?}", |
| 134 | + trait_datum.associated_ty_ids |
| 135 | + ); |
| 136 | + let metadata_id = trait_datum.associated_ty_ids[0]; |
| 137 | + let alias_last_field = AliasTy::Projection(ProjectionTy { |
| 138 | + associated_ty_id: metadata_id, |
| 139 | + substitution: Substitution::from1(interner, last_field_ty), |
| 140 | + }); |
| 141 | + let alias_self = AliasTy::Projection(ProjectionTy { |
| 142 | + associated_ty_id: metadata_id, |
| 143 | + substitution: Substitution::from1(interner, self_ty), |
| 144 | + }); |
| 145 | + builder.push_fact(Normalize { |
| 146 | + alias: alias_self, |
| 147 | + ty: TyKind::Alias(alias_last_field).intern(interner), |
| 148 | + }); |
| 149 | +} |
0 commit comments