Skip to content

Commit 32497c7

Browse files
author
Nathan Hawes
committed
Check '= super' is actually parsed correctly in module interfaces via an execution test
Also pass the decls themselves rather than their locations when diagnosing incorrect usage of '= super'.
1 parent d4f1894 commit 32497c7

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,19 +1760,17 @@ checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
17601760
auto ctor = dyn_cast<ConstructorDecl>(DC);
17611761
if (!ctor || ctor->isConvenienceInit()) {
17621762
TC.diagnose(
1763-
PD->getLoc(),
1764-
diag::inherited_default_value_not_in_designated_constructor);
1763+
PD, diag::inherited_default_value_not_in_designated_constructor);
17651764
return;
17661765
}
17671766

17681767
// The decl it overrides should also be a designated initializer.
17691768
auto overridden = ctor->getOverriddenDecl();
17701769
if (!overridden || overridden->isConvenienceInit()) {
17711770
TC.diagnose(
1772-
PD->getLoc(),
1773-
diag::inherited_default_value_used_in_non_overriding_constructor);
1771+
PD, diag::inherited_default_value_used_in_non_overriding_constructor);
17741772
if (overridden)
1775-
TC.diagnose(overridden->getLoc(), diag::overridden_here);
1773+
TC.diagnose(overridden, diag::overridden_here);
17761774
return;
17771775
}
17781776

@@ -1781,8 +1779,8 @@ checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
17811779
assert(idx && "containing decl does not contain param?");
17821780
ParamDecl *equivalentParam = overridden->getParameters()->get(*idx);
17831781
if (equivalentParam->getDefaultArgumentKind() == DefaultArgumentKind::None) {
1784-
TC.diagnose(PD->getLoc(), diag::corresponding_param_not_defaulted);
1785-
TC.diagnose(equivalentParam->getLoc(), diag::inherited_default_param_here);
1782+
TC.diagnose(PD, diag::corresponding_param_not_defaulted);
1783+
TC.diagnose(equivalentParam, diag::inherited_default_param_here);
17861784
}
17871785
}
17881786

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// REQUIRES: executable_test
2+
// RUN: %empty-directory(%t)
3+
4+
// 1) Build the 'Inherited' library and its interface from this file
5+
//
6+
// RUN: %target-build-swift-dylib(%t/%target-library-name(Inherited)) -emit-module-path %t/Inherited.swiftmodule -emit-parseable-module-interface-path %t/Inherited.swiftinterface -module-name Inherited %s
7+
// RUN: rm %t/Inherited.swiftmodule
8+
9+
// 2) Check the interface includes the synthesized initializers of the base
10+
// class in the derived class explicitly and uses the '= super' syntax to
11+
// inherit its default arguments.
12+
//
13+
// RUN: cat %t/Inherited.swiftinterface | %FileCheck --check-prefix=INTERFACE %s
14+
//
15+
// INTERFACE: public class Base {
16+
// INTERFACE: public init(x: Swift.Int = 45, y: Swift.Int = 98)
17+
// INTERFACE: }
18+
// INTERFACE: public class Derived : Inherited.Base {
19+
// INTERFACE: override public init(x: Swift.Int = super, y: Swift.Int = super)
20+
// INTERFACE: }
21+
22+
// 4) Generate a main.swift file that uses the 'Inherited' library and makes use
23+
// of the inherited default arguments
24+
//
25+
// RUN: echo "import Inherited" > %t/main.swift
26+
// RUN: echo "print(Derived().x)" >> %t/main.swift
27+
// RUN: echo "print(Derived().y)" >> %t/main.swift
28+
29+
// 5) Build and run the executable, checking the defaulted arguments resulted in
30+
// the correct values being stored
31+
//
32+
// RUN: %target-build-swift -I%t -L%t -lInherited -o %t/main %target-rpath(%t) %t/main.swift -swift-version 5
33+
// RUN: %target-codesign %t/main %t/%target-library-name(Inherited)
34+
// RUN: %target-run %t/main | %FileCheck --check-prefix=OUTPUT %s
35+
//
36+
// OUTPUT: 45
37+
// OUTPUT-NEXT: 98
38+
39+
public class Base {
40+
public let x: Int
41+
public let y: Int
42+
public init(x: Int = 45, y: Int = 98) {
43+
self.x = x
44+
self.y = y
45+
}
46+
}
47+
public class Derived: Base {}

0 commit comments

Comments
 (0)