1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
//! This is the home of `Ty` etc. until they get replaced by their chalk_ir //! equivalents. use std::sync::Arc; use chalk_ir::{ cast::{CastTo, Caster}, BoundVar, Mutability, Scalar, TyVariableKind, }; use hir_def::LifetimeParamId; use smallvec::SmallVec; use crate::{ AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, FnDefId, FnSig, ForeignDefId, InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, }; #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { Parameter(LifetimeParamId), Static, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct OpaqueTy { pub opaque_ty_id: OpaqueTyId, pub substitution: Substitution, } /// A "projection" type corresponds to an (unnormalized) /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the /// trait and all its parameters are fully known. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct ProjectionTy { pub associated_ty_id: AssocTypeId, pub substitution: Substitution, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct DynTy { /// The unknown self type. pub bounds: Binders<QuantifiedWhereClauses>, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct FnPointer { pub num_args: usize, pub sig: FnSig, pub substs: Substitution, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum AliasTy { /// A "projection" type corresponds to an (unnormalized) /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the /// trait and all its parameters are fully known. Projection(ProjectionTy), /// An opaque type (`impl Trait`). /// /// This is currently only used for return type impl trait; each instance of /// `impl Trait` in a return type gets its own ID. Opaque(OpaqueTy), } /// A type. /// /// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents /// the same thing (but in a different way). /// /// This should be cheap to clone. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum TyKind { /// Structures, enumerations and unions. Adt(chalk_ir::AdtId<Interner>, Substitution), /// Represents an associated item like `Iterator::Item`. This is used /// when we have tried to normalize a projection like `T::Item` but /// couldn't find a better representation. In that case, we generate /// an **application type** like `(Iterator::Item)<T>`. AssociatedType(AssocTypeId, Substitution), /// a scalar type like `bool` or `u32` Scalar(Scalar), /// A tuple type. For example, `(i32, bool)`. Tuple(usize, Substitution), /// An array with the given length. Written as `[T; n]`. Array(Ty), /// The pointee of an array slice. Written as `[T]`. Slice(Ty), /// A raw pointer. Written as `*mut T` or `*const T` Raw(Mutability, Ty), /// A reference; a pointer with an associated lifetime. Written as /// `&'a mut T` or `&'a T`. Ref(Mutability, Ty), /// This represents a placeholder for an opaque type in situations where we /// don't know the hidden type (i.e. currently almost always). This is /// analogous to the `AssociatedType` type constructor. /// It is also used as the type of async block, with one type parameter /// representing the Future::Output type. OpaqueType(OpaqueTyId, Substitution), /// The anonymous type of a function declaration/definition. Each /// function has a unique type, which is output (for a function /// named `foo` returning an `i32`) as `fn() -> i32 {foo}`. /// /// This includes tuple struct / enum variant constructors as well. /// /// For example the type of `bar` here: /// /// ``` /// fn foo() -> i32 { 1 } /// let bar = foo; // bar: fn() -> i32 {foo} /// ``` FnDef(FnDefId, Substitution), /// The pointee of a string slice. Written as `str`. Str, /// The never type `!`. Never, /// The type of a specific closure. /// /// The closure signature is stored in a `FnPtr` type in the first type /// parameter. Closure(ClosureId, Substitution), /// Represents a foreign type declared in external blocks. ForeignType(ForeignDefId), /// A pointer to a function. Written as `fn() -> i32`. /// /// For example the type of `bar` here: /// /// ``` /// fn foo() -> i32 { 1 } /// let bar: fn() -> i32 = foo; /// ``` Function(FnPointer), /// An "alias" type represents some form of type alias, such as: /// - An associated type projection like `<T as Iterator>::Item` /// - `impl Trait` types /// - Named type aliases like `type Foo<X> = Vec<X>` Alias(AliasTy), /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T) /// {}` when we're type-checking the body of that function. In this /// situation, we know this stands for *some* type, but don't know the exact /// type. Placeholder(PlaceholderIndex), /// A bound type variable. This is used in various places: when representing /// some polymorphic type like the type of function `fn f<T>`, the type /// parameters get turned into variables; during trait resolution, inference /// variables get turned into bound variables and back; and in `Dyn` the /// `Self` type is represented with a bound variable as well. BoundVar(BoundVar), /// A type variable used during type checking. InferenceVar(InferenceVar, TyVariableKind), /// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust). /// /// The predicates are quantified over the `Self` type, i.e. `Ty::Bound(0)` /// represents the `Self` type inside the bounds. This is currently /// implicit; Chalk has the `Binders` struct to make it explicit, but it /// didn't seem worth the overhead yet. Dyn(DynTy), /// A placeholder for a type which could not be computed; this is propagated /// to avoid useless error messages. Doubles as a placeholder where type /// variables are inserted before type checking, since we want to try to /// infer a better type here anyway -- for the IDE use case, we want to try /// to infer as much as possible even in the presence of type errors. Unknown, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Ty(Arc<TyKind>); impl TyKind { pub fn intern(self, _interner: &Interner) -> Ty { Ty(Arc::new(self)) } } impl Ty { pub fn kind(&self, _interner: &Interner) -> &TyKind { &self.0 } pub fn interned_mut(&mut self) -> &mut TyKind { Arc::make_mut(&mut self.0) } pub fn into_inner(self) -> TyKind { Arc::try_unwrap(self.0).unwrap_or_else(|a| (*a).clone()) } } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct GenericArg { interned: GenericArgData, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum GenericArgData { Ty(Ty), } impl GenericArg { /// Constructs a generic argument using `GenericArgData`. pub fn new(_interner: &Interner, data: GenericArgData) -> Self { GenericArg { interned: data } } /// Gets the interned value. pub fn interned(&self) -> &GenericArgData { &self.interned } /// Asserts that this is a type argument. pub fn assert_ty_ref(&self, interner: &Interner) -> &Ty { self.ty(interner).unwrap() } /// Checks whether the generic argument is a type. pub fn is_ty(&self, _interner: &Interner) -> bool { match self.interned() { GenericArgData::Ty(_) => true, } } /// Returns the type if it is one, `None` otherwise. pub fn ty(&self, _interner: &Interner) -> Option<&Ty> { match self.interned() { GenericArgData::Ty(t) => Some(t), } } pub fn interned_mut(&mut self) -> &mut GenericArgData { &mut self.interned } } /// A list of substitutions for generic parameters. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Substitution(SmallVec<[GenericArg; 2]>); impl Substitution { pub fn interned(&self) -> &[GenericArg] { &self.0 } pub fn len(&self, _: &Interner) -> usize { self.0.len() } pub fn is_empty(&self, _: &Interner) -> bool { self.0.is_empty() } pub fn at(&self, _: &Interner, i: usize) -> &GenericArg { &self.0[i] } pub fn empty(_: &Interner) -> Substitution { Substitution(SmallVec::new()) } pub fn iter(&self, _: &Interner) -> std::slice::Iter<'_, GenericArg> { self.0.iter() } pub fn from_iter( interner: &Interner, elements: impl IntoIterator<Item = impl CastTo<GenericArg>>, ) -> Self { Substitution(elements.into_iter().casted(interner).collect()) } // We can hopefully add this to Chalk pub fn intern(interned: SmallVec<[GenericArg; 2]>) -> Substitution { Substitution(interned) } pub fn interned_mut(&mut self) -> &mut SmallVec<[GenericArg; 2]> { &mut self.0 } } #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Binders<T> { pub num_binders: usize, pub value: T, } /// A trait with type parameters. This includes the `Self`, so this represents a concrete type implementing the trait. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct TraitRef { pub trait_id: ChalkTraitId, pub substitution: Substitution, } /// Like `generics::WherePredicate`, but with resolved types: A condition on the /// parameters of a generic item. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum WhereClause { /// The given trait needs to be implemented for its type parameters. Implemented(TraitRef), /// An associated type bindings like in `Iterator<Item = T>`. AliasEq(AliasEq), } pub type QuantifiedWhereClause = Binders<WhereClause>; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct QuantifiedWhereClauses(Arc<[QuantifiedWhereClause]>); impl QuantifiedWhereClauses { pub fn from_iter( _interner: &Interner, elements: impl IntoIterator<Item = QuantifiedWhereClause>, ) -> Self { QuantifiedWhereClauses(elements.into_iter().collect()) } pub fn interned(&self) -> &Arc<[QuantifiedWhereClause]> { &self.0 } pub fn interned_mut(&mut self) -> &mut Arc<[QuantifiedWhereClause]> { &mut self.0 } } /// Basically a claim (currently not validated / checked) that the contained /// type / trait ref contains no inference variables; any inference variables it /// contained have been replaced by bound variables, and `kinds` tells us how /// many there are and whether they were normal or float/int variables. This is /// used to erase irrelevant differences between types before using them in /// queries. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Canonical<T> { pub value: T, pub binders: CanonicalVarKinds, } /// Something (usually a goal), along with an environment. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct InEnvironment<T> { pub environment: chalk_ir::Environment<Interner>, pub goal: T, } impl<T> InEnvironment<T> { pub fn new(environment: chalk_ir::Environment<Interner>, value: T) -> InEnvironment<T> { InEnvironment { environment, goal: value } } } /// Something that needs to be proven (by Chalk) during type checking, e.g. that /// a certain type implements a certain trait. Proving the Obligation might /// result in additional information about inference variables. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum DomainGoal { Holds(WhereClause), } #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct AliasEq { pub alias: AliasTy, pub ty: Ty, } #[derive(Clone, Debug, PartialEq, Eq)] pub struct SolutionVariables(pub Canonical<Substitution>); #[derive(Clone, Debug, PartialEq, Eq)] /// A (possible) solution for a proposed goal. pub enum Solution { /// The goal indeed holds, and there is a unique value for all existential /// variables. Unique(SolutionVariables), /// The goal may be provable in multiple ways, but regardless we may have some guidance /// for type inference. In this case, we don't return any lifetime /// constraints, since we have not "committed" to any particular solution /// yet. Ambig(Guidance), } #[derive(Clone, Debug, PartialEq, Eq)] /// When a goal holds ambiguously (e.g., because there are multiple possible /// solutions), we issue a set of *guidance* back to type inference. pub enum Guidance { /// The existential variables *must* have the given values if the goal is /// ever to hold, but that alone isn't enough to guarantee the goal will /// actually hold. Definite(SolutionVariables), /// There are multiple plausible values for the existentials, but the ones /// here are suggested as the preferred choice heuristically. These should /// be used for inference fallback only. Suggested(SolutionVariables), /// There's no useful information to feed back to type inference Unknown, }