@@ -1765,6 +1765,82 @@ static void validatePrecedenceGroup(PrecedenceGroupDecl *PGD) {
1765
1765
}
1766
1766
}
1767
1767
1768
+ static Optional<unsigned >
1769
+ getParamIndex (const ParameterList *paramList, const ParamDecl *decl) {
1770
+ ArrayRef<ParamDecl *> params = paramList->getArray ();
1771
+ for (unsigned i = 0 ; i < params.size (); ++i) {
1772
+ if (params[i] == decl) return i;
1773
+ }
1774
+ return None;
1775
+ }
1776
+
1777
+ static void
1778
+ checkInheritedDefaultValueRestrictions (TypeChecker &TC, ParamDecl *PD) {
1779
+ if (PD->getDefaultArgumentKind () != DefaultArgumentKind::Inherited)
1780
+ return ;
1781
+
1782
+ auto *DC = PD->getInnermostDeclContext ();
1783
+ const SourceFile *SF = DC->getParentSourceFile ();
1784
+ assert ((SF && SF->Kind == SourceFileKind::Interface || PD->isImplicit ()) &&
1785
+ " explicit inherited default argument outside of a module interface?" );
1786
+
1787
+ // The containing decl should be a designated initializer.
1788
+ auto ctor = dyn_cast<ConstructorDecl>(DC);
1789
+ if (!ctor || ctor->isConvenienceInit ()) {
1790
+ TC.diagnose (
1791
+ PD, diag::inherited_default_value_not_in_designated_constructor);
1792
+ return ;
1793
+ }
1794
+
1795
+ // The decl it overrides should also be a designated initializer.
1796
+ auto overridden = ctor->getOverriddenDecl ();
1797
+ if (!overridden || overridden->isConvenienceInit ()) {
1798
+ TC.diagnose (
1799
+ PD, diag::inherited_default_value_used_in_non_overriding_constructor);
1800
+ if (overridden)
1801
+ TC.diagnose (overridden, diag::overridden_here);
1802
+ return ;
1803
+ }
1804
+
1805
+ // The corresponding parameter should have a default value.
1806
+ Optional<unsigned > idx = getParamIndex (ctor->getParameters (), PD);
1807
+ assert (idx && " containing decl does not contain param?" );
1808
+ ParamDecl *equivalentParam = overridden->getParameters ()->get (*idx);
1809
+ if (equivalentParam->getDefaultArgumentKind () == DefaultArgumentKind::None) {
1810
+ TC.diagnose (PD, diag::corresponding_param_not_defaulted);
1811
+ TC.diagnose (equivalentParam, diag::inherited_default_param_here);
1812
+ }
1813
+ }
1814
+
1815
+ // / Check the default arguments that occur within this pattern.
1816
+ static void checkDefaultArguments (TypeChecker &tc, ParameterList *params,
1817
+ ValueDecl *VD) {
1818
+ for (auto *param : *params) {
1819
+ checkInheritedDefaultValueRestrictions (tc, param);
1820
+ if (!param->getDefaultValue () ||
1821
+ !param->hasInterfaceType () ||
1822
+ param->getInterfaceType ()->hasError ())
1823
+ continue ;
1824
+
1825
+ Expr *e = param->getDefaultValue ();
1826
+ auto *initContext = param->getDefaultArgumentInitContext ();
1827
+
1828
+ auto resultTy =
1829
+ tc.typeCheckParameterDefault (e, initContext, param->getType (),
1830
+ /* isAutoClosure=*/ param->isAutoClosure ());
1831
+
1832
+ if (resultTy) {
1833
+ param->setDefaultValue (e);
1834
+ }
1835
+
1836
+ tc.checkInitializerErrorHandling (initContext, e);
1837
+
1838
+ // Walk the checked initializer and contextualize any closures
1839
+ // we saw there.
1840
+ (void )tc.contextualizeInitializer (initContext, e);
1841
+ }
1842
+ }
1843
+
1768
1844
PrecedenceGroupDecl *TypeChecker::lookupPrecedenceGroup (DeclContext *dc,
1769
1845
Identifier name,
1770
1846
SourceLoc nameLoc) {
@@ -2384,7 +2460,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2384
2460
(void ) SD->getImplInfo ();
2385
2461
2386
2462
TC.checkParameterAttributes (SD->getIndices ());
2387
- TC. checkDefaultArguments (SD->getIndices (), SD);
2463
+ checkDefaultArguments (TC, SD->getIndices (), SD);
2388
2464
2389
2465
if (SD->getDeclContext ()->getSelfClassDecl ()) {
2390
2466
checkDynamicSelfType (SD, SD->getValueInterfaceType ());
@@ -2982,6 +3058,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2982
3058
2983
3059
if (FD->getDeclContext ()->getSelfClassDecl ())
2984
3060
checkDynamicSelfType (FD, FD->getResultInterfaceType ());
3061
+
3062
+ checkDefaultArguments (TC, FD->getParameters (), FD);
2985
3063
}
2986
3064
2987
3065
void visitModuleDecl (ModuleDecl *) { }
@@ -2997,7 +3075,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2997
3075
2998
3076
if (auto *PL = EED->getParameterList ()) {
2999
3077
TC.checkParameterAttributes (PL);
3000
- TC. checkDefaultArguments (PL, EED);
3078
+ checkDefaultArguments (TC, PL, EED);
3001
3079
}
3002
3080
3003
3081
// Yell if our parent doesn't have a raw type but we have a raw value.
@@ -3252,6 +3330,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
3252
3330
} else {
3253
3331
TC.definedFunctions .push_back (CD);
3254
3332
}
3333
+
3334
+ checkDefaultArguments (TC, CD->getParameters (), CD);
3255
3335
}
3256
3336
3257
3337
void visitDestructorDecl (DestructorDecl *DD) {
0 commit comments