diff options
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 340 |
1 files changed, 166 insertions, 174 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index c87789d45..a035686bc 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -8,14 +8,14 @@ | |||
8 | use std::{iter, sync::Arc}; | 8 | use std::{iter, sync::Arc}; |
9 | 9 | ||
10 | use base_db::CrateId; | 10 | use base_db::CrateId; |
11 | use chalk_ir::{cast::Cast, Mutability, Safety}; | 11 | use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety}; |
12 | use hir_def::{ | 12 | use hir_def::{ |
13 | adt::StructKind, | 13 | adt::StructKind, |
14 | builtin_type::BuiltinType, | 14 | builtin_type::BuiltinType, |
15 | generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, | 15 | generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, |
16 | path::{GenericArg, Path, PathSegment, PathSegments}, | 16 | path::{GenericArg, Path, PathSegment, PathSegments}, |
17 | resolver::{HasResolver, Resolver, TypeNs}, | 17 | resolver::{HasResolver, Resolver, TypeNs}, |
18 | type_ref::{TypeBound, TypeRef}, | 18 | type_ref::{TraitRef as HirTraitRef, TypeBound, TypeRef}, |
19 | AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, | 19 | AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, |
20 | GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, | 20 | GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, |
21 | TypeAliasId, TypeParamId, UnionId, VariantId, | 21 | TypeAliasId, TypeParamId, UnionId, VariantId, |
@@ -27,16 +27,16 @@ use stdx::impl_from; | |||
27 | 27 | ||
28 | use crate::{ | 28 | use crate::{ |
29 | db::HirDatabase, | 29 | db::HirDatabase, |
30 | to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, | 30 | dummy_usize_const, |
31 | traits::chalk::{Interner, ToChalk}, | 31 | mapping::ToChalk, |
32 | static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, | ||
32 | utils::{ | 33 | utils::{ |
33 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, | 34 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, |
34 | variant_data, | ||
35 | }, | 35 | }, |
36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, | 36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, |
37 | ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, | 37 | FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, |
38 | ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty, | 38 | QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, |
39 | TyKind, TypeWalk, WhereClause, | 39 | TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, |
40 | }; | 40 | }; |
41 | 41 | ||
42 | #[derive(Debug)] | 42 | #[derive(Debug)] |
@@ -146,7 +146,7 @@ impl<'a> TyLoweringContext<'a> { | |||
146 | self.lower_ty_ext(type_ref).0 | 146 | self.lower_ty_ext(type_ref).0 |
147 | } | 147 | } |
148 | 148 | ||
149 | fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) { | 149 | pub fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) { |
150 | let mut res = None; | 150 | let mut res = None; |
151 | let ty = match type_ref { | 151 | let ty = match type_ref { |
152 | TypeRef::Never => TyKind::Never.intern(&Interner), | 152 | TypeRef::Never => TyKind::Never.intern(&Interner), |
@@ -166,7 +166,7 @@ impl<'a> TyLoweringContext<'a> { | |||
166 | } | 166 | } |
167 | TypeRef::Array(inner) => { | 167 | TypeRef::Array(inner) => { |
168 | let inner_ty = self.lower_ty(inner); | 168 | let inner_ty = self.lower_ty(inner); |
169 | TyKind::Array(inner_ty).intern(&Interner) | 169 | TyKind::Array(inner_ty, dummy_usize_const()).intern(&Interner) |
170 | } | 170 | } |
171 | TypeRef::Slice(inner) => { | 171 | TypeRef::Slice(inner) => { |
172 | let inner_ty = self.lower_ty(inner); | 172 | let inner_ty = self.lower_ty(inner); |
@@ -174,15 +174,19 @@ impl<'a> TyLoweringContext<'a> { | |||
174 | } | 174 | } |
175 | TypeRef::Reference(inner, _, mutability) => { | 175 | TypeRef::Reference(inner, _, mutability) => { |
176 | let inner_ty = self.lower_ty(inner); | 176 | let inner_ty = self.lower_ty(inner); |
177 | TyKind::Ref(lower_to_chalk_mutability(*mutability), inner_ty).intern(&Interner) | 177 | let lifetime = static_lifetime(); |
178 | TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty) | ||
179 | .intern(&Interner) | ||
178 | } | 180 | } |
179 | TypeRef::Placeholder => TyKind::Unknown.intern(&Interner), | 181 | TypeRef::Placeholder => TyKind::Error.intern(&Interner), |
180 | TypeRef::Fn(params, is_varargs) => { | 182 | TypeRef::Fn(params, is_varargs) => { |
181 | let substs = Substitution(params.iter().map(|tr| self.lower_ty(tr)).collect()); | 183 | let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
184 | Substitution::from_iter(&Interner, params.iter().map(|tr| ctx.lower_ty(tr))) | ||
185 | }); | ||
182 | TyKind::Function(FnPointer { | 186 | TyKind::Function(FnPointer { |
183 | num_args: substs.len() - 1, | 187 | num_binders: 0, // FIXME lower `for<'a> fn()` correctly |
184 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: *is_varargs }, | 188 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: *is_varargs }, |
185 | substs, | 189 | substitution: FnSubst(substs), |
186 | }) | 190 | }) |
187 | .intern(&Interner) | 191 | .intern(&Interner) |
188 | } | 192 | } |
@@ -195,8 +199,8 @@ impl<'a> TyLoweringContext<'a> { | |||
195 | bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)), | 199 | bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)), |
196 | ) | 200 | ) |
197 | }); | 201 | }); |
198 | let bounds = Binders::new(1, bounds); | 202 | let bounds = crate::make_only_type_binders(1, bounds); |
199 | TyKind::Dyn(DynTy { bounds }).intern(&Interner) | 203 | TyKind::Dyn(DynTy { bounds, lifetime: static_lifetime() }).intern(&Interner) |
200 | } | 204 | } |
201 | TypeRef::ImplTrait(bounds) => { | 205 | TypeRef::ImplTrait(bounds) => { |
202 | match self.impl_trait_mode { | 206 | match self.impl_trait_mode { |
@@ -208,9 +212,9 @@ impl<'a> TyLoweringContext<'a> { | |||
208 | // this dance is to make sure the data is in the right | 212 | // this dance is to make sure the data is in the right |
209 | // place even if we encounter more opaque types while | 213 | // place even if we encounter more opaque types while |
210 | // lowering the bounds | 214 | // lowering the bounds |
211 | self.opaque_type_data | 215 | self.opaque_type_data.borrow_mut().push(ReturnTypeImplTrait { |
212 | .borrow_mut() | 216 | bounds: crate::make_only_type_binders(1, Vec::new()), |
213 | .push(ReturnTypeImplTrait { bounds: Binders::new(1, Vec::new()) }); | 217 | }); |
214 | // We don't want to lower the bounds inside the binders | 218 | // We don't want to lower the bounds inside the binders |
215 | // we're currently in, because they don't end up inside | 219 | // we're currently in, because they don't end up inside |
216 | // those binders. E.g. when we have `impl Trait<impl | 220 | // those binders. E.g. when we have `impl Trait<impl |
@@ -233,7 +237,7 @@ impl<'a> TyLoweringContext<'a> { | |||
233 | let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); | 237 | let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); |
234 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); | 238 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); |
235 | let generics = generics(self.db.upcast(), func.into()); | 239 | let generics = generics(self.db.upcast(), func.into()); |
236 | let parameters = Substitution::bound_vars(&generics, self.in_binders); | 240 | let parameters = generics.bound_vars_subst(self.in_binders); |
237 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { | 241 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { |
238 | opaque_ty_id, | 242 | opaque_ty_id, |
239 | substitution: parameters, | 243 | substitution: parameters, |
@@ -252,12 +256,12 @@ impl<'a> TyLoweringContext<'a> { | |||
252 | data.provenance == TypeParamProvenance::ArgumentImplTrait | 256 | data.provenance == TypeParamProvenance::ArgumentImplTrait |
253 | }) | 257 | }) |
254 | .nth(idx as usize) | 258 | .nth(idx as usize) |
255 | .map_or(TyKind::Unknown, |(id, _)| { | 259 | .map_or(TyKind::Error, |(id, _)| { |
256 | TyKind::Placeholder(to_placeholder_idx(self.db, id)) | 260 | TyKind::Placeholder(to_placeholder_idx(self.db, id)) |
257 | }); | 261 | }); |
258 | param.intern(&Interner) | 262 | param.intern(&Interner) |
259 | } else { | 263 | } else { |
260 | TyKind::Unknown.intern(&Interner) | 264 | TyKind::Error.intern(&Interner) |
261 | } | 265 | } |
262 | } | 266 | } |
263 | ImplTraitLoweringMode::Variable => { | 267 | ImplTraitLoweringMode::Variable => { |
@@ -279,11 +283,11 @@ impl<'a> TyLoweringContext<'a> { | |||
279 | } | 283 | } |
280 | ImplTraitLoweringMode::Disallowed => { | 284 | ImplTraitLoweringMode::Disallowed => { |
281 | // FIXME: report error | 285 | // FIXME: report error |
282 | TyKind::Unknown.intern(&Interner) | 286 | TyKind::Error.intern(&Interner) |
283 | } | 287 | } |
284 | } | 288 | } |
285 | } | 289 | } |
286 | TypeRef::Error => TyKind::Unknown.intern(&Interner), | 290 | TypeRef::Error => TyKind::Error.intern(&Interner), |
287 | }; | 291 | }; |
288 | (ty, res) | 292 | (ty, res) |
289 | } | 293 | } |
@@ -327,7 +331,7 @@ impl<'a> TyLoweringContext<'a> { | |||
327 | (self.select_associated_type(res, segment), None) | 331 | (self.select_associated_type(res, segment), None) |
328 | } else if remaining_segments.len() > 1 { | 332 | } else if remaining_segments.len() > 1 { |
329 | // FIXME report error (ambiguous associated type) | 333 | // FIXME report error (ambiguous associated type) |
330 | (TyKind::Unknown.intern(&Interner), None) | 334 | (TyKind::Error.intern(&Interner), None) |
331 | } else { | 335 | } else { |
332 | (ty, res) | 336 | (ty, res) |
333 | } | 337 | } |
@@ -371,21 +375,24 @@ impl<'a> TyLoweringContext<'a> { | |||
371 | } | 375 | } |
372 | None => { | 376 | None => { |
373 | // FIXME: report error (associated type not found) | 377 | // FIXME: report error (associated type not found) |
374 | TyKind::Unknown.intern(&Interner) | 378 | TyKind::Error.intern(&Interner) |
375 | } | 379 | } |
376 | } | 380 | } |
377 | } else if remaining_segments.len() > 1 { | 381 | } else if remaining_segments.len() > 1 { |
378 | // FIXME report error (ambiguous associated type) | 382 | // FIXME report error (ambiguous associated type) |
379 | TyKind::Unknown.intern(&Interner) | 383 | TyKind::Error.intern(&Interner) |
380 | } else { | 384 | } else { |
381 | let dyn_ty = DynTy { | 385 | let dyn_ty = DynTy { |
382 | bounds: Binders::new( | 386 | bounds: crate::make_only_type_binders( |
383 | 1, | 387 | 1, |
384 | QuantifiedWhereClauses::from_iter( | 388 | QuantifiedWhereClauses::from_iter( |
385 | &Interner, | 389 | &Interner, |
386 | Some(Binders::wrap_empty(WhereClause::Implemented(trait_ref))), | 390 | Some(crate::wrap_empty_binders(WhereClause::Implemented( |
391 | trait_ref, | ||
392 | ))), | ||
387 | ), | 393 | ), |
388 | ), | 394 | ), |
395 | lifetime: static_lifetime(), | ||
389 | }; | 396 | }; |
390 | TyKind::Dyn(dyn_ty).intern(&Interner) | 397 | TyKind::Dyn(dyn_ty).intern(&Interner) |
391 | }; | 398 | }; |
@@ -410,26 +417,18 @@ impl<'a> TyLoweringContext<'a> { | |||
410 | TypeNs::SelfType(impl_id) => { | 417 | TypeNs::SelfType(impl_id) => { |
411 | let generics = generics(self.db.upcast(), impl_id.into()); | 418 | let generics = generics(self.db.upcast(), impl_id.into()); |
412 | let substs = match self.type_param_mode { | 419 | let substs = match self.type_param_mode { |
413 | TypeParamLoweringMode::Placeholder => { | 420 | TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), |
414 | Substitution::type_params_for_generics(self.db, &generics) | 421 | TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), |
415 | } | ||
416 | TypeParamLoweringMode::Variable => { | ||
417 | Substitution::bound_vars(&generics, self.in_binders) | ||
418 | } | ||
419 | }; | 422 | }; |
420 | self.db.impl_self_ty(impl_id).subst(&substs) | 423 | self.db.impl_self_ty(impl_id).substitute(&Interner, &substs) |
421 | } | 424 | } |
422 | TypeNs::AdtSelfType(adt) => { | 425 | TypeNs::AdtSelfType(adt) => { |
423 | let generics = generics(self.db.upcast(), adt.into()); | 426 | let generics = generics(self.db.upcast(), adt.into()); |
424 | let substs = match self.type_param_mode { | 427 | let substs = match self.type_param_mode { |
425 | TypeParamLoweringMode::Placeholder => { | 428 | TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), |
426 | Substitution::type_params_for_generics(self.db, &generics) | 429 | TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), |
427 | } | ||
428 | TypeParamLoweringMode::Variable => { | ||
429 | Substitution::bound_vars(&generics, self.in_binders) | ||
430 | } | ||
431 | }; | 430 | }; |
432 | self.db.ty(adt.into()).subst(&substs) | 431 | self.db.ty(adt.into()).substitute(&Interner, &substs) |
433 | } | 432 | } |
434 | 433 | ||
435 | TypeNs::AdtId(it) => self.lower_path_inner(resolved_segment, it.into(), infer_args), | 434 | TypeNs::AdtId(it) => self.lower_path_inner(resolved_segment, it.into(), infer_args), |
@@ -440,7 +439,7 @@ impl<'a> TyLoweringContext<'a> { | |||
440 | self.lower_path_inner(resolved_segment, it.into(), infer_args) | 439 | self.lower_path_inner(resolved_segment, it.into(), infer_args) |
441 | } | 440 | } |
442 | // FIXME: report error | 441 | // FIXME: report error |
443 | TypeNs::EnumVariantId(_) => return (TyKind::Unknown.intern(&Interner), None), | 442 | TypeNs::EnumVariantId(_) => return (TyKind::Error.intern(&Interner), None), |
444 | }; | 443 | }; |
445 | self.lower_ty_relative_path(ty, Some(resolution), remaining_segments) | 444 | self.lower_ty_relative_path(ty, Some(resolution), remaining_segments) |
446 | } | 445 | } |
@@ -454,7 +453,7 @@ impl<'a> TyLoweringContext<'a> { | |||
454 | let (resolution, remaining_index) = | 453 | let (resolution, remaining_index) = |
455 | match self.resolver.resolve_path_in_type_ns(self.db.upcast(), path.mod_path()) { | 454 | match self.resolver.resolve_path_in_type_ns(self.db.upcast(), path.mod_path()) { |
456 | Some(it) => it, | 455 | Some(it) => it, |
457 | None => return (TyKind::Unknown.intern(&Interner), None), | 456 | None => return (TyKind::Error.intern(&Interner), None), |
458 | }; | 457 | }; |
459 | let (resolved_segment, remaining_segments) = match remaining_index { | 458 | let (resolved_segment, remaining_segments) = match remaining_index { |
460 | None => ( | 459 | None => ( |
@@ -477,19 +476,20 @@ impl<'a> TyLoweringContext<'a> { | |||
477 | TypeParamLoweringMode::Placeholder => { | 476 | TypeParamLoweringMode::Placeholder => { |
478 | // if we're lowering to placeholders, we have to put | 477 | // if we're lowering to placeholders, we have to put |
479 | // them in now | 478 | // them in now |
480 | let s = Substitution::type_params( | 479 | let generics = generics( |
481 | self.db, | 480 | self.db.upcast(), |
482 | self.resolver.generic_def().expect( | 481 | self.resolver.generic_def().expect( |
483 | "there should be generics if there's a generic param", | 482 | "there should be generics if there's a generic param", |
484 | ), | 483 | ), |
485 | ); | 484 | ); |
486 | t.substitution.clone().subst_bound_vars(&s) | 485 | let s = generics.type_params_subst(self.db); |
486 | s.apply(t.substitution.clone(), &Interner) | ||
487 | } | 487 | } |
488 | TypeParamLoweringMode::Variable => t.substitution.clone(), | 488 | TypeParamLoweringMode::Variable => t.substitution.clone(), |
489 | }; | 489 | }; |
490 | // We need to shift in the bound vars, since | 490 | // We need to shift in the bound vars, since |
491 | // associated_type_shorthand_candidates does not do that | 491 | // associated_type_shorthand_candidates does not do that |
492 | let substs = substs.shift_bound_vars(self.in_binders); | 492 | let substs = substs.shifted_in_from(&Interner, self.in_binders); |
493 | // FIXME handle type parameters on the segment | 493 | // FIXME handle type parameters on the segment |
494 | return Some( | 494 | return Some( |
495 | TyKind::Alias(AliasTy::Projection(ProjectionTy { | 495 | TyKind::Alias(AliasTy::Projection(ProjectionTy { |
@@ -504,9 +504,9 @@ impl<'a> TyLoweringContext<'a> { | |||
504 | }, | 504 | }, |
505 | ); | 505 | ); |
506 | 506 | ||
507 | ty.unwrap_or(TyKind::Unknown.intern(&Interner)) | 507 | ty.unwrap_or(TyKind::Error.intern(&Interner)) |
508 | } else { | 508 | } else { |
509 | TyKind::Unknown.intern(&Interner) | 509 | TyKind::Error.intern(&Interner) |
510 | } | 510 | } |
511 | } | 511 | } |
512 | 512 | ||
@@ -522,7 +522,7 @@ impl<'a> TyLoweringContext<'a> { | |||
522 | TyDefId::TypeAliasId(it) => Some(it.into()), | 522 | TyDefId::TypeAliasId(it) => Some(it.into()), |
523 | }; | 523 | }; |
524 | let substs = self.substs_from_path_segment(segment, generic_def, infer_args, None); | 524 | let substs = self.substs_from_path_segment(segment, generic_def, infer_args, None); |
525 | self.db.ty(typeable).subst(&substs) | 525 | self.db.ty(typeable).substitute(&Interner, &substs) |
526 | } | 526 | } |
527 | 527 | ||
528 | /// Collect generic arguments from a path into a `Substs`. See also | 528 | /// Collect generic arguments from a path into a `Substs`. See also |
@@ -575,13 +575,13 @@ impl<'a> TyLoweringContext<'a> { | |||
575 | def_generics.map_or((0, 0, 0, 0), |g| g.provenance_split()); | 575 | def_generics.map_or((0, 0, 0, 0), |g| g.provenance_split()); |
576 | let total_len = parent_params + self_params + type_params + impl_trait_params; | 576 | let total_len = parent_params + self_params + type_params + impl_trait_params; |
577 | 577 | ||
578 | substs.extend(iter::repeat(TyKind::Unknown.intern(&Interner)).take(parent_params)); | 578 | substs.extend(iter::repeat(TyKind::Error.intern(&Interner)).take(parent_params)); |
579 | 579 | ||
580 | let fill_self_params = || { | 580 | let fill_self_params = || { |
581 | substs.extend( | 581 | substs.extend( |
582 | explicit_self_ty | 582 | explicit_self_ty |
583 | .into_iter() | 583 | .into_iter() |
584 | .chain(iter::repeat(TyKind::Unknown.intern(&Interner))) | 584 | .chain(iter::repeat(TyKind::Error.intern(&Interner))) |
585 | .take(self_params), | 585 | .take(self_params), |
586 | ) | 586 | ) |
587 | }; | 587 | }; |
@@ -625,8 +625,8 @@ impl<'a> TyLoweringContext<'a> { | |||
625 | 625 | ||
626 | for default_ty in defaults.iter().skip(substs.len()) { | 626 | for default_ty in defaults.iter().skip(substs.len()) { |
627 | // each default can depend on the previous parameters | 627 | // each default can depend on the previous parameters |
628 | let substs_so_far = Substitution(substs.clone().into()); | 628 | let substs_so_far = Substitution::from_iter(&Interner, substs.clone()); |
629 | substs.push(default_ty.clone().subst(&substs_so_far)); | 629 | substs.push(default_ty.clone().substitute(&Interner, &substs_so_far)); |
630 | } | 630 | } |
631 | } | 631 | } |
632 | } | 632 | } |
@@ -634,11 +634,11 @@ impl<'a> TyLoweringContext<'a> { | |||
634 | // add placeholders for args that were not provided | 634 | // add placeholders for args that were not provided |
635 | // FIXME: emit diagnostics in contexts where this is not allowed | 635 | // FIXME: emit diagnostics in contexts where this is not allowed |
636 | for _ in substs.len()..total_len { | 636 | for _ in substs.len()..total_len { |
637 | substs.push(TyKind::Unknown.intern(&Interner)); | 637 | substs.push(TyKind::Error.intern(&Interner)); |
638 | } | 638 | } |
639 | assert_eq!(substs.len(), total_len); | 639 | assert_eq!(substs.len(), total_len); |
640 | 640 | ||
641 | Substitution(substs.into()) | 641 | Substitution::from_iter(&Interner, substs) |
642 | } | 642 | } |
643 | 643 | ||
644 | fn lower_trait_ref_from_path( | 644 | fn lower_trait_ref_from_path( |
@@ -667,14 +667,10 @@ impl<'a> TyLoweringContext<'a> { | |||
667 | 667 | ||
668 | fn lower_trait_ref( | 668 | fn lower_trait_ref( |
669 | &self, | 669 | &self, |
670 | type_ref: &TypeRef, | 670 | trait_ref: &HirTraitRef, |
671 | explicit_self_ty: Option<Ty>, | 671 | explicit_self_ty: Option<Ty>, |
672 | ) -> Option<TraitRef> { | 672 | ) -> Option<TraitRef> { |
673 | let path = match type_ref { | 673 | self.lower_trait_ref_from_path(&trait_ref.path, explicit_self_ty) |
674 | TypeRef::Path(path) => path, | ||
675 | _ => return None, | ||
676 | }; | ||
677 | self.lower_trait_ref_from_path(path, explicit_self_ty) | ||
678 | } | 674 | } |
679 | 675 | ||
680 | fn trait_ref_substs_from_path( | 676 | fn trait_ref_substs_from_path( |
@@ -730,7 +726,7 @@ impl<'a> TyLoweringContext<'a> { | |||
730 | let trait_ref = match bound { | 726 | let trait_ref = match bound { |
731 | TypeBound::Path(path) => { | 727 | TypeBound::Path(path) => { |
732 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); | 728 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); |
733 | bindings.clone().map(WhereClause::Implemented).map(|b| Binders::wrap_empty(b)) | 729 | bindings.clone().map(WhereClause::Implemented).map(|b| crate::wrap_empty_binders(b)) |
734 | } | 730 | } |
735 | TypeBound::Lifetime(_) => None, | 731 | TypeBound::Lifetime(_) => None, |
736 | TypeBound::Error => None, | 732 | TypeBound::Error => None, |
@@ -777,7 +773,7 @@ impl<'a> TyLoweringContext<'a> { | |||
777 | let ty = self.lower_ty(type_ref); | 773 | let ty = self.lower_ty(type_ref); |
778 | let alias_eq = | 774 | let alias_eq = |
779 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; | 775 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; |
780 | preds.push(Binders::wrap_empty(WhereClause::AliasEq(alias_eq))); | 776 | preds.push(crate::wrap_empty_binders(WhereClause::AliasEq(alias_eq))); |
781 | } | 777 | } |
782 | for bound in &binding.bounds { | 778 | for bound in &binding.bounds { |
783 | preds.extend(self.lower_type_bound( | 779 | preds.extend(self.lower_type_bound( |
@@ -797,7 +793,7 @@ impl<'a> TyLoweringContext<'a> { | |||
797 | let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 793 | let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
798 | bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)).collect() | 794 | bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)).collect() |
799 | }); | 795 | }); |
800 | ReturnTypeImplTrait { bounds: Binders::new(1, predicates) } | 796 | ReturnTypeImplTrait { bounds: crate::make_only_type_binders(1, predicates) } |
801 | } | 797 | } |
802 | } | 798 | } |
803 | 799 | ||
@@ -825,58 +821,57 @@ pub fn associated_type_shorthand_candidates<R>( | |||
825 | res: TypeNs, | 821 | res: TypeNs, |
826 | mut cb: impl FnMut(&Name, &TraitRef, TypeAliasId) -> Option<R>, | 822 | mut cb: impl FnMut(&Name, &TraitRef, TypeAliasId) -> Option<R>, |
827 | ) -> Option<R> { | 823 | ) -> Option<R> { |
828 | let traits_from_env: Vec<_> = match res { | 824 | let mut search = |t| { |
829 | TypeNs::SelfType(impl_id) => match db.impl_trait(impl_id) { | 825 | for t in all_super_trait_refs(db, t) { |
830 | None => vec![], | 826 | let data = db.trait_data(t.hir_trait_id()); |
831 | // FIXME: how to correctly handle higher-ranked bounds here? | 827 | |
832 | Some(trait_ref) => vec![trait_ref.value.shift_bound_vars_out(DebruijnIndex::ONE)], | 828 | for (name, assoc_id) in &data.items { |
833 | }, | 829 | if let AssocItemId::TypeAliasId(alias) = assoc_id { |
830 | if let Some(result) = cb(name, &t, *alias) { | ||
831 | return Some(result); | ||
832 | } | ||
833 | } | ||
834 | } | ||
835 | } | ||
836 | None | ||
837 | }; | ||
838 | |||
839 | match res { | ||
840 | TypeNs::SelfType(impl_id) => search( | ||
841 | // we're _in_ the impl -- the binders get added back later. Correct, | ||
842 | // but it would be nice to make this more explicit | ||
843 | db.impl_trait(impl_id)?.into_value_and_skipped_binders().0, | ||
844 | ), | ||
834 | TypeNs::GenericParam(param_id) => { | 845 | TypeNs::GenericParam(param_id) => { |
835 | let predicates = db.generic_predicates_for_param(param_id); | 846 | let predicates = db.generic_predicates_for_param(param_id); |
836 | let mut traits_: Vec<_> = predicates | 847 | let res = predicates.iter().find_map(|pred| match pred.skip_binders().skip_binders() { |
837 | .iter() | 848 | // FIXME: how to correctly handle higher-ranked bounds here? |
838 | .filter_map(|pred| match &pred.value.value { | 849 | WhereClause::Implemented(tr) => search( |
839 | // FIXME: how to correctly handle higher-ranked bounds here? | 850 | tr.clone() |
840 | WhereClause::Implemented(tr) => { | 851 | .shifted_out_to(&Interner, DebruijnIndex::ONE) |
841 | Some(tr.clone().shift_bound_vars_out(DebruijnIndex::ONE)) | 852 | .expect("FIXME unexpected higher-ranked trait bound"), |
842 | } | 853 | ), |
843 | _ => None, | 854 | _ => None, |
844 | }) | 855 | }); |
845 | .collect(); | 856 | if let res @ Some(_) = res { |
857 | return res; | ||
858 | } | ||
846 | // Handle `Self::Type` referring to own associated type in trait definitions | 859 | // Handle `Self::Type` referring to own associated type in trait definitions |
847 | if let GenericDefId::TraitId(trait_id) = param_id.parent { | 860 | if let GenericDefId::TraitId(trait_id) = param_id.parent { |
848 | let generics = generics(db.upcast(), trait_id.into()); | 861 | let generics = generics(db.upcast(), trait_id.into()); |
849 | if generics.params.types[param_id.local_id].provenance | 862 | if generics.params.types[param_id.local_id].provenance |
850 | == TypeParamProvenance::TraitSelf | 863 | == TypeParamProvenance::TraitSelf |
851 | { | 864 | { |
852 | let trait_ref = TraitRef { | 865 | let trait_ref = TyBuilder::trait_ref(db, trait_id) |
853 | trait_id: to_chalk_trait_id(trait_id), | 866 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) |
854 | substitution: Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST), | 867 | .build(); |
855 | }; | 868 | return search(trait_ref); |
856 | traits_.push(trait_ref); | ||
857 | } | 869 | } |
858 | } | 870 | } |
859 | traits_ | 871 | None |
860 | } | ||
861 | _ => vec![], | ||
862 | }; | ||
863 | |||
864 | for t in traits_from_env.into_iter().flat_map(move |t| all_super_trait_refs(db, t)) { | ||
865 | let data = db.trait_data(t.hir_trait_id()); | ||
866 | |||
867 | for (name, assoc_id) in &data.items { | ||
868 | match assoc_id { | ||
869 | AssocItemId::TypeAliasId(alias) => { | ||
870 | if let Some(result) = cb(name, &t, *alias) { | ||
871 | return Some(result); | ||
872 | } | ||
873 | } | ||
874 | AssocItemId::FunctionId(_) | AssocItemId::ConstId(_) => {} | ||
875 | } | ||
876 | } | 872 | } |
873 | _ => None, | ||
877 | } | 874 | } |
878 | |||
879 | None | ||
880 | } | 875 | } |
881 | 876 | ||
882 | /// Build the type of all specific fields of a struct or enum variant. | 877 | /// Build the type of all specific fields of a struct or enum variant. |
@@ -884,7 +879,7 @@ pub(crate) fn field_types_query( | |||
884 | db: &dyn HirDatabase, | 879 | db: &dyn HirDatabase, |
885 | variant_id: VariantId, | 880 | variant_id: VariantId, |
886 | ) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>> { | 881 | ) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>> { |
887 | let var_data = variant_data(db.upcast(), variant_id); | 882 | let var_data = variant_id.variant_data(db.upcast()); |
888 | let (resolver, def): (_, GenericDefId) = match variant_id { | 883 | let (resolver, def): (_, GenericDefId) = match variant_id { |
889 | VariantId::StructId(it) => (it.resolver(db.upcast()), it.into()), | 884 | VariantId::StructId(it) => (it.resolver(db.upcast()), it.into()), |
890 | VariantId::UnionId(it) => (it.resolver(db.upcast()), it.into()), | 885 | VariantId::UnionId(it) => (it.resolver(db.upcast()), it.into()), |
@@ -895,7 +890,7 @@ pub(crate) fn field_types_query( | |||
895 | let ctx = | 890 | let ctx = |
896 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 891 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
897 | for (field_id, field_data) in var_data.fields().iter() { | 892 | for (field_id, field_data) in var_data.fields().iter() { |
898 | res.insert(field_id, Binders::new(generics.len(), ctx.lower_ty(&field_data.type_ref))) | 893 | res.insert(field_id, make_binders(&generics, ctx.lower_ty(&field_data.type_ref))) |
899 | } | 894 | } |
900 | Arc::new(res) | 895 | Arc::new(res) |
901 | } | 896 | } |
@@ -929,9 +924,7 @@ pub(crate) fn generic_predicates_for_param_query( | |||
929 | }, | 924 | }, |
930 | WherePredicate::Lifetime { .. } => false, | 925 | WherePredicate::Lifetime { .. } => false, |
931 | }) | 926 | }) |
932 | .flat_map(|pred| { | 927 | .flat_map(|pred| ctx.lower_where_predicate(pred, true).map(|p| make_binders(&generics, p))) |
933 | ctx.lower_where_predicate(pred, true).map(|p| Binders::new(generics.len(), p)) | ||
934 | }) | ||
935 | .collect() | 928 | .collect() |
936 | } | 929 | } |
937 | 930 | ||
@@ -955,10 +948,10 @@ pub(crate) fn trait_environment_query( | |||
955 | for pred in resolver.where_predicates_in_scope() { | 948 | for pred in resolver.where_predicates_in_scope() { |
956 | for pred in ctx.lower_where_predicate(pred, false) { | 949 | for pred in ctx.lower_where_predicate(pred, false) { |
957 | if let WhereClause::Implemented(tr) = &pred.skip_binders() { | 950 | if let WhereClause::Implemented(tr) = &pred.skip_binders() { |
958 | traits_in_scope.push((tr.self_type_parameter().clone(), tr.hir_trait_id())); | 951 | traits_in_scope |
952 | .push((tr.self_type_parameter(&Interner).clone(), tr.hir_trait_id())); | ||
959 | } | 953 | } |
960 | let program_clause: chalk_ir::ProgramClause<Interner> = | 954 | let program_clause: chalk_ir::ProgramClause<Interner> = pred.clone().cast(&Interner); |
961 | pred.clone().to_chalk(db).cast(&Interner); | ||
962 | clauses.push(program_clause.into_from_env_clause(&Interner)); | 955 | clauses.push(program_clause.into_from_env_clause(&Interner)); |
963 | } | 956 | } |
964 | } | 957 | } |
@@ -978,10 +971,10 @@ pub(crate) fn trait_environment_query( | |||
978 | // function default implementations (and hypothetical code | 971 | // function default implementations (and hypothetical code |
979 | // inside consts or type aliases) | 972 | // inside consts or type aliases) |
980 | cov_mark::hit!(trait_self_implements_self); | 973 | cov_mark::hit!(trait_self_implements_self); |
981 | let substs = Substitution::type_params(db, trait_id); | 974 | let substs = TyBuilder::type_params_subst(db, trait_id); |
982 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs }; | 975 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs }; |
983 | let pred = WhereClause::Implemented(trait_ref); | 976 | let pred = WhereClause::Implemented(trait_ref); |
984 | let program_clause: chalk_ir::ProgramClause<Interner> = pred.to_chalk(db).cast(&Interner); | 977 | let program_clause: chalk_ir::ProgramClause<Interner> = pred.cast(&Interner); |
985 | clauses.push(program_clause.into_from_env_clause(&Interner)); | 978 | clauses.push(program_clause.into_from_env_clause(&Interner)); |
986 | } | 979 | } |
987 | 980 | ||
@@ -1001,9 +994,7 @@ pub(crate) fn generic_predicates_query( | |||
1001 | let generics = generics(db.upcast(), def); | 994 | let generics = generics(db.upcast(), def); |
1002 | resolver | 995 | resolver |
1003 | .where_predicates_in_scope() | 996 | .where_predicates_in_scope() |
1004 | .flat_map(|pred| { | 997 | .flat_map(|pred| ctx.lower_where_predicate(pred, false).map(|p| make_binders(&generics, p))) |
1005 | ctx.lower_where_predicate(pred, false).map(|p| Binders::new(generics.len(), p)) | ||
1006 | }) | ||
1007 | .collect() | 998 | .collect() |
1008 | } | 999 | } |
1009 | 1000 | ||
@@ -1022,25 +1013,21 @@ pub(crate) fn generic_defaults_query( | |||
1022 | .enumerate() | 1013 | .enumerate() |
1023 | .map(|(idx, (_, p))| { | 1014 | .map(|(idx, (_, p))| { |
1024 | let mut ty = | 1015 | let mut ty = |
1025 | p.default.as_ref().map_or(TyKind::Unknown.intern(&Interner), |t| ctx.lower_ty(t)); | 1016 | p.default.as_ref().map_or(TyKind::Error.intern(&Interner), |t| ctx.lower_ty(t)); |
1026 | 1017 | ||
1027 | // Each default can only refer to previous parameters. | 1018 | // Each default can only refer to previous parameters. |
1028 | ty.walk_mut_binders( | 1019 | ty = crate::fold_free_vars(ty, |bound, binders| { |
1029 | &mut |ty, binders| match ty.interned_mut() { | 1020 | if bound.index >= idx && bound.debruijn == DebruijnIndex::INNERMOST { |
1030 | TyKind::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => { | 1021 | // type variable default referring to parameter coming |
1031 | if *index >= idx { | 1022 | // after it. This is forbidden (FIXME: report |
1032 | // type variable default referring to parameter coming | 1023 | // diagnostic) |
1033 | // after it. This is forbidden (FIXME: report | 1024 | TyKind::Error.intern(&Interner) |
1034 | // diagnostic) | 1025 | } else { |
1035 | *ty = TyKind::Unknown.intern(&Interner); | 1026 | bound.shifted_in_from(binders).to_ty(&Interner) |
1036 | } | 1027 | } |
1037 | } | 1028 | }); |
1038 | _ => {} | ||
1039 | }, | ||
1040 | DebruijnIndex::INNERMOST, | ||
1041 | ); | ||
1042 | 1029 | ||
1043 | Binders::new(idx, ty) | 1030 | crate::make_only_type_binders(idx, ty) |
1044 | }) | 1031 | }) |
1045 | .collect(); | 1032 | .collect(); |
1046 | 1033 | ||
@@ -1053,23 +1040,22 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1053 | let ctx_params = TyLoweringContext::new(db, &resolver) | 1040 | let ctx_params = TyLoweringContext::new(db, &resolver) |
1054 | .with_impl_trait_mode(ImplTraitLoweringMode::Variable) | 1041 | .with_impl_trait_mode(ImplTraitLoweringMode::Variable) |
1055 | .with_type_param_mode(TypeParamLoweringMode::Variable); | 1042 | .with_type_param_mode(TypeParamLoweringMode::Variable); |
1056 | let params = data.params.iter().map(|tr| (&ctx_params).lower_ty(tr)).collect::<Vec<_>>(); | 1043 | let params = data.params.iter().map(|tr| ctx_params.lower_ty(tr)).collect::<Vec<_>>(); |
1057 | let ctx_ret = TyLoweringContext::new(db, &resolver) | 1044 | let ctx_ret = TyLoweringContext::new(db, &resolver) |
1058 | .with_impl_trait_mode(ImplTraitLoweringMode::Opaque) | 1045 | .with_impl_trait_mode(ImplTraitLoweringMode::Opaque) |
1059 | .with_type_param_mode(TypeParamLoweringMode::Variable); | 1046 | .with_type_param_mode(TypeParamLoweringMode::Variable); |
1060 | let ret = (&ctx_ret).lower_ty(&data.ret_type); | 1047 | let ret = ctx_ret.lower_ty(&data.ret_type); |
1061 | let generics = generics(db.upcast(), def.into()); | 1048 | let generics = generics(db.upcast(), def.into()); |
1062 | let num_binders = generics.len(); | 1049 | make_binders(&generics, CallableSig::from_params_and_return(params, ret, data.is_varargs())) |
1063 | Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs)) | ||
1064 | } | 1050 | } |
1065 | 1051 | ||
1066 | /// Build the declared type of a function. This should not need to look at the | 1052 | /// Build the declared type of a function. This should not need to look at the |
1067 | /// function body. | 1053 | /// function body. |
1068 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { | 1054 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { |
1069 | let generics = generics(db.upcast(), def.into()); | 1055 | let generics = generics(db.upcast(), def.into()); |
1070 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1056 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1071 | Binders::new( | 1057 | make_binders( |
1072 | substs.len(), | 1058 | &generics, |
1073 | TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), | 1059 | TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), |
1074 | ) | 1060 | ) |
1075 | } | 1061 | } |
@@ -1082,7 +1068,7 @@ fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> { | |||
1082 | let ctx = | 1068 | let ctx = |
1083 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1069 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1084 | 1070 | ||
1085 | Binders::new(generics.len(), ctx.lower_ty(&data.type_ref)) | 1071 | make_binders(&generics, ctx.lower_ty(&data.type_ref)) |
1086 | } | 1072 | } |
1087 | 1073 | ||
1088 | /// Build the declared type of a static. | 1074 | /// Build the declared type of a static. |
@@ -1091,7 +1077,7 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> { | |||
1091 | let resolver = def.resolver(db.upcast()); | 1077 | let resolver = def.resolver(db.upcast()); |
1092 | let ctx = TyLoweringContext::new(db, &resolver); | 1078 | let ctx = TyLoweringContext::new(db, &resolver); |
1093 | 1079 | ||
1094 | Binders::new(0, ctx.lower_ty(&data.type_ref)) | 1080 | Binders::empty(&Interner, ctx.lower_ty(&data.type_ref)) |
1095 | } | 1081 | } |
1096 | 1082 | ||
1097 | fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { | 1083 | fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { |
@@ -1101,8 +1087,8 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS | |||
1101 | let ctx = | 1087 | let ctx = |
1102 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1088 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1103 | let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>(); | 1089 | let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>(); |
1104 | let ret = type_for_adt(db, def.into()); | 1090 | let (ret, binders) = type_for_adt(db, def.into()).into_value_and_skipped_binders(); |
1105 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) | 1091 | Binders::new(binders, CallableSig::from_params_and_return(params, ret, false)) |
1106 | } | 1092 | } |
1107 | 1093 | ||
1108 | /// Build the type of a tuple struct constructor. | 1094 | /// Build the type of a tuple struct constructor. |
@@ -1112,9 +1098,9 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T | |||
1112 | return type_for_adt(db, def.into()); | 1098 | return type_for_adt(db, def.into()); |
1113 | } | 1099 | } |
1114 | let generics = generics(db.upcast(), def.into()); | 1100 | let generics = generics(db.upcast(), def.into()); |
1115 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1101 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1116 | Binders::new( | 1102 | make_binders( |
1117 | substs.len(), | 1103 | &generics, |
1118 | TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), | 1104 | TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), |
1119 | ) | 1105 | ) |
1120 | } | 1106 | } |
@@ -1127,8 +1113,8 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) | |||
1127 | let ctx = | 1113 | let ctx = |
1128 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1114 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1129 | let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>(); | 1115 | let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>(); |
1130 | let ret = type_for_adt(db, def.parent.into()); | 1116 | let (ret, binders) = type_for_adt(db, def.parent.into()).into_value_and_skipped_binders(); |
1131 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) | 1117 | Binders::new(binders, CallableSig::from_params_and_return(params, ret, false)) |
1132 | } | 1118 | } |
1133 | 1119 | ||
1134 | /// Build the type of a tuple enum variant constructor. | 1120 | /// Build the type of a tuple enum variant constructor. |
@@ -1139,17 +1125,18 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1139 | return type_for_adt(db, def.parent.into()); | 1125 | return type_for_adt(db, def.parent.into()); |
1140 | } | 1126 | } |
1141 | let generics = generics(db.upcast(), def.parent.into()); | 1127 | let generics = generics(db.upcast(), def.parent.into()); |
1142 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1128 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1143 | Binders::new( | 1129 | make_binders( |
1144 | substs.len(), | 1130 | &generics, |
1145 | TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), | 1131 | TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), |
1146 | ) | 1132 | ) |
1147 | } | 1133 | } |
1148 | 1134 | ||
1149 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { | 1135 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { |
1150 | let generics = generics(db.upcast(), adt.into()); | 1136 | let generics = generics(db.upcast(), adt.into()); |
1151 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1137 | let b = TyBuilder::adt(db, adt); |
1152 | Binders::new(substs.len(), Ty::adt_ty(adt, substs)) | 1138 | let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build(); |
1139 | make_binders(&generics, ty) | ||
1153 | } | 1140 | } |
1154 | 1141 | ||
1155 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | 1142 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { |
@@ -1158,11 +1145,11 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | |||
1158 | let ctx = | 1145 | let ctx = |
1159 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1146 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1160 | if db.type_alias_data(t).is_extern { | 1147 | if db.type_alias_data(t).is_extern { |
1161 | Binders::new(0, TyKind::ForeignType(crate::to_foreign_def_id(t)).intern(&Interner)) | 1148 | Binders::empty(&Interner, TyKind::Foreign(crate::to_foreign_def_id(t)).intern(&Interner)) |
1162 | } else { | 1149 | } else { |
1163 | let type_ref = &db.type_alias_data(t).type_ref; | 1150 | let type_ref = &db.type_alias_data(t).type_ref; |
1164 | let inner = ctx.lower_ty(type_ref.as_ref().unwrap_or(&TypeRef::Error)); | 1151 | let inner = ctx.lower_ty(type_ref.as_deref().unwrap_or(&TypeRef::Error)); |
1165 | Binders::new(generics.len(), inner) | 1152 | make_binders(&generics, inner) |
1166 | } | 1153 | } |
1167 | } | 1154 | } |
1168 | 1155 | ||
@@ -1221,19 +1208,21 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V | |||
1221 | /// namespace. | 1208 | /// namespace. |
1222 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { | 1209 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { |
1223 | match def { | 1210 | match def { |
1224 | TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)), | 1211 | TyDefId::BuiltinType(it) => Binders::empty(&Interner, TyBuilder::builtin(it)), |
1225 | TyDefId::AdtId(it) => type_for_adt(db, it), | 1212 | TyDefId::AdtId(it) => type_for_adt(db, it), |
1226 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), | 1213 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), |
1227 | } | 1214 | } |
1228 | } | 1215 | } |
1229 | 1216 | ||
1230 | pub(crate) fn ty_recover(db: &dyn HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> { | 1217 | pub(crate) fn ty_recover(db: &dyn HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> { |
1231 | let num_binders = match *def { | 1218 | let generics = match *def { |
1232 | TyDefId::BuiltinType(_) => 0, | 1219 | TyDefId::BuiltinType(_) => { |
1233 | TyDefId::AdtId(it) => generics(db.upcast(), it.into()).len(), | 1220 | return Binders::empty(&Interner, TyKind::Error.intern(&Interner)) |
1234 | TyDefId::TypeAliasId(it) => generics(db.upcast(), it.into()).len(), | 1221 | } |
1222 | TyDefId::AdtId(it) => generics(db.upcast(), it.into()), | ||
1223 | TyDefId::TypeAliasId(it) => generics(db.upcast(), it.into()), | ||
1235 | }; | 1224 | }; |
1236 | Binders::new(num_binders, TyKind::Unknown.intern(&Interner)) | 1225 | make_binders(&generics, TyKind::Error.intern(&Interner)) |
1237 | } | 1226 | } |
1238 | 1227 | ||
1239 | pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Binders<Ty> { | 1228 | pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Binders<Ty> { |
@@ -1253,7 +1242,7 @@ pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binde | |||
1253 | let generics = generics(db.upcast(), impl_id.into()); | 1242 | let generics = generics(db.upcast(), impl_id.into()); |
1254 | let ctx = | 1243 | let ctx = |
1255 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1244 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1256 | Binders::new(generics.len(), ctx.lower_ty(&impl_data.target_type)) | 1245 | make_binders(&generics, ctx.lower_ty(&impl_data.self_ty)) |
1257 | } | 1246 | } |
1258 | 1247 | ||
1259 | pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty { | 1248 | pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty { |
@@ -1271,7 +1260,7 @@ pub(crate) fn impl_self_ty_recover( | |||
1271 | impl_id: &ImplId, | 1260 | impl_id: &ImplId, |
1272 | ) -> Binders<Ty> { | 1261 | ) -> Binders<Ty> { |
1273 | let generics = generics(db.upcast(), (*impl_id).into()); | 1262 | let generics = generics(db.upcast(), (*impl_id).into()); |
1274 | Binders::new(generics.len(), TyKind::Unknown.intern(&Interner)) | 1263 | make_binders(&generics, TyKind::Error.intern(&Interner)) |
1275 | } | 1264 | } |
1276 | 1265 | ||
1277 | pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> { | 1266 | pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> { |
@@ -1279,9 +1268,9 @@ pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option< | |||
1279 | let resolver = impl_id.resolver(db.upcast()); | 1268 | let resolver = impl_id.resolver(db.upcast()); |
1280 | let ctx = | 1269 | let ctx = |
1281 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1270 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1282 | let self_ty = db.impl_self_ty(impl_id); | 1271 | let (self_ty, binders) = db.impl_self_ty(impl_id).into_value_and_skipped_binders(); |
1283 | let target_trait = impl_data.target_trait.as_ref()?; | 1272 | let target_trait = impl_data.target_trait.as_ref()?; |
1284 | Some(Binders::new(self_ty.num_binders, ctx.lower_trait_ref(target_trait, Some(self_ty.value))?)) | 1273 | Some(Binders::new(binders, ctx.lower_trait_ref(target_trait, Some(self_ty))?)) |
1285 | } | 1274 | } |
1286 | 1275 | ||
1287 | pub(crate) fn return_type_impl_traits( | 1276 | pub(crate) fn return_type_impl_traits( |
@@ -1296,13 +1285,12 @@ pub(crate) fn return_type_impl_traits( | |||
1296 | .with_type_param_mode(TypeParamLoweringMode::Variable); | 1285 | .with_type_param_mode(TypeParamLoweringMode::Variable); |
1297 | let _ret = (&ctx_ret).lower_ty(&data.ret_type); | 1286 | let _ret = (&ctx_ret).lower_ty(&data.ret_type); |
1298 | let generics = generics(db.upcast(), def.into()); | 1287 | let generics = generics(db.upcast(), def.into()); |
1299 | let num_binders = generics.len(); | ||
1300 | let return_type_impl_traits = | 1288 | let return_type_impl_traits = |
1301 | ReturnTypeImplTraits { impl_traits: ctx_ret.opaque_type_data.into_inner() }; | 1289 | ReturnTypeImplTraits { impl_traits: ctx_ret.opaque_type_data.into_inner() }; |
1302 | if return_type_impl_traits.impl_traits.is_empty() { | 1290 | if return_type_impl_traits.impl_traits.is_empty() { |
1303 | None | 1291 | None |
1304 | } else { | 1292 | } else { |
1305 | Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) | 1293 | Some(Arc::new(make_binders(&generics, return_type_impl_traits))) |
1306 | } | 1294 | } |
1307 | } | 1295 | } |
1308 | 1296 | ||
@@ -1312,3 +1300,7 @@ pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mut | |||
1312 | hir_def::type_ref::Mutability::Mut => Mutability::Mut, | 1300 | hir_def::type_ref::Mutability::Mut => Mutability::Mut, |
1313 | } | 1301 | } |
1314 | } | 1302 | } |
1303 | |||
1304 | fn make_binders<T: HasInterner<Interner = Interner>>(generics: &Generics, value: T) -> Binders<T> { | ||
1305 | crate::make_only_type_binders(generics.len(), value) | ||
1306 | } | ||