|
8 | 8 | use std::{
|
9 | 9 | cell::{Cell, RefCell, RefMut},
|
10 | 10 | iter,
|
11 |
| - ops::Not as _, |
| 11 | + ops::{self, Not as _}, |
12 | 12 | };
|
13 | 13 |
|
14 | 14 | use base_db::{
|
@@ -1693,9 +1693,11 @@ pub(crate) fn trait_environment_query(
|
1693 | 1693 | #[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
1694 | 1694 | pub struct GenericPredicates(Option<Arc<[Binders<QuantifiedWhereClause>]>>);
|
1695 | 1695 |
|
1696 |
| -impl GenericPredicates { |
1697 |
| - pub fn iter(&self) -> impl Iterator<Item = &Binders<QuantifiedWhereClause>> + '_ + Clone { |
1698 |
| - self.0.as_ref().into_iter().flat_map(Arc::as_ref) |
| 1696 | +impl ops::Deref for GenericPredicates { |
| 1697 | + type Target = [Binders<crate::QuantifiedWhereClause>]; |
| 1698 | + |
| 1699 | + fn deref(&self) -> &Self::Target { |
| 1700 | + self.0.as_deref().unwrap_or(&[]) |
1699 | 1701 | }
|
1700 | 1702 | }
|
1701 | 1703 |
|
@@ -1767,68 +1769,84 @@ fn implicitly_sized_clauses<'a, 'subst: 'a>(
|
1767 | 1769 | })
|
1768 | 1770 | }
|
1769 | 1771 |
|
| 1772 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 1773 | +pub struct GenericDefaults(Option<Arc<[Binders<crate::GenericArg>]>>); |
| 1774 | + |
| 1775 | +impl ops::Deref for GenericDefaults { |
| 1776 | + type Target = [Binders<crate::GenericArg>]; |
| 1777 | + |
| 1778 | + fn deref(&self) -> &Self::Target { |
| 1779 | + self.0.as_deref().unwrap_or(&[]) |
| 1780 | + } |
| 1781 | +} |
| 1782 | + |
1770 | 1783 | /// Resolve the default type params from generics
|
1771 |
| -pub(crate) fn generic_defaults_query( |
1772 |
| - db: &dyn HirDatabase, |
1773 |
| - def: GenericDefId, |
1774 |
| -) -> Arc<[Binders<crate::GenericArg>]> { |
1775 |
| - let resolver = def.resolver(db.upcast()); |
| 1784 | +pub(crate) fn generic_defaults_query(db: &dyn HirDatabase, def: GenericDefId) -> GenericDefaults { |
1776 | 1785 | let generic_params = generics(db.upcast(), def);
|
| 1786 | + if generic_params.len() == 0 { |
| 1787 | + return GenericDefaults(None); |
| 1788 | + } |
| 1789 | + let resolver = def.resolver(db.upcast()); |
1777 | 1790 | let parent_start_idx = generic_params.len_self();
|
1778 | 1791 |
|
1779 | 1792 | let ctx = TyLoweringContext::new(db, &resolver, def.into())
|
1780 | 1793 | .with_impl_trait_mode(ImplTraitLoweringMode::Disallowed)
|
1781 | 1794 | .with_type_param_mode(ParamLoweringMode::Variable);
|
1782 |
| - Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| { |
1783 |
| - match p { |
1784 |
| - GenericParamDataRef::TypeParamData(p) => { |
1785 |
| - let mut ty = |
1786 |
| - p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t)); |
1787 |
| - // Each default can only refer to previous parameters. |
1788 |
| - // Type variable default referring to parameter coming |
1789 |
| - // after it is forbidden (FIXME: report diagnostic) |
1790 |
| - ty = fallback_bound_vars(ty, idx, parent_start_idx); |
1791 |
| - crate::make_binders(db, &generic_params, ty.cast(Interner)) |
1792 |
| - } |
1793 |
| - GenericParamDataRef::ConstParamData(p) => { |
1794 |
| - let GenericParamId::ConstParamId(id) = id else { |
1795 |
| - unreachable!("Unexpected lifetime or type argument") |
1796 |
| - }; |
| 1795 | + GenericDefaults(Some(Arc::from_iter(generic_params.iter().enumerate().map( |
| 1796 | + |(idx, (id, p))| { |
| 1797 | + match p { |
| 1798 | + GenericParamDataRef::TypeParamData(p) => { |
| 1799 | + let ty = p.default.as_ref().map_or(TyKind::Error.intern(Interner), |ty| { |
| 1800 | + // Each default can only refer to previous parameters. |
| 1801 | + // Type variable default referring to parameter coming |
| 1802 | + // after it is forbidden (FIXME: report diagnostic) |
| 1803 | + fallback_bound_vars(ctx.lower_ty(ty), idx, parent_start_idx) |
| 1804 | + }); |
| 1805 | + crate::make_binders(db, &generic_params, ty.cast(Interner)) |
| 1806 | + } |
| 1807 | + GenericParamDataRef::ConstParamData(p) => { |
| 1808 | + let GenericParamId::ConstParamId(id) = id else { |
| 1809 | + unreachable!("Unexpected lifetime or type argument") |
| 1810 | + }; |
1797 | 1811 |
|
1798 |
| - let mut val = p.default.as_ref().map_or_else( |
1799 |
| - || unknown_const_as_generic(db.const_param_ty(id)), |
1800 |
| - |c| { |
1801 |
| - let c = ctx.lower_const(c, ctx.lower_ty(&p.ty)); |
1802 |
| - c.cast(Interner) |
1803 |
| - }, |
1804 |
| - ); |
1805 |
| - // Each default can only refer to previous parameters, see above. |
1806 |
| - val = fallback_bound_vars(val, idx, parent_start_idx); |
1807 |
| - make_binders(db, &generic_params, val) |
1808 |
| - } |
1809 |
| - GenericParamDataRef::LifetimeParamData(_) => { |
1810 |
| - make_binders(db, &generic_params, error_lifetime().cast(Interner)) |
| 1812 | + let mut val = p.default.as_ref().map_or_else( |
| 1813 | + || unknown_const_as_generic(db.const_param_ty(id)), |
| 1814 | + |c| { |
| 1815 | + let c = ctx.lower_const(c, ctx.lower_ty(&p.ty)); |
| 1816 | + c.cast(Interner) |
| 1817 | + }, |
| 1818 | + ); |
| 1819 | + // Each default can only refer to previous parameters, see above. |
| 1820 | + val = fallback_bound_vars(val, idx, parent_start_idx); |
| 1821 | + make_binders(db, &generic_params, val) |
| 1822 | + } |
| 1823 | + GenericParamDataRef::LifetimeParamData(_) => { |
| 1824 | + make_binders(db, &generic_params, error_lifetime().cast(Interner)) |
| 1825 | + } |
1811 | 1826 | }
|
1812 |
| - } |
1813 |
| - })) |
| 1827 | + }, |
| 1828 | + )))) |
1814 | 1829 | }
|
1815 | 1830 |
|
1816 | 1831 | pub(crate) fn generic_defaults_recover(
|
1817 | 1832 | db: &dyn HirDatabase,
|
1818 | 1833 | _cycle: &Cycle,
|
1819 | 1834 | def: &GenericDefId,
|
1820 |
| -) -> Arc<[Binders<crate::GenericArg>]> { |
| 1835 | +) -> GenericDefaults { |
1821 | 1836 | let generic_params = generics(db.upcast(), *def);
|
| 1837 | + if generic_params.len() == 0 { |
| 1838 | + return GenericDefaults(None); |
| 1839 | + } |
1822 | 1840 | // FIXME: this code is not covered in tests.
|
1823 | 1841 | // we still need one default per parameter
|
1824 |
| - Arc::from_iter(generic_params.iter_id().map(|id| { |
| 1842 | + GenericDefaults(Some(Arc::from_iter(generic_params.iter_id().map(|id| { |
1825 | 1843 | let val = match id {
|
1826 | 1844 | GenericParamId::TypeParamId(_) => TyKind::Error.intern(Interner).cast(Interner),
|
1827 | 1845 | GenericParamId::ConstParamId(id) => unknown_const_as_generic(db.const_param_ty(id)),
|
1828 | 1846 | GenericParamId::LifetimeParamId(_) => error_lifetime().cast(Interner),
|
1829 | 1847 | };
|
1830 | 1848 | crate::make_binders(db, &generic_params, val)
|
1831 |
| - })) |
| 1849 | + })))) |
1832 | 1850 | }
|
1833 | 1851 |
|
1834 | 1852 | fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
|
|
0 commit comments