Skip to content

Commit fc2bc07

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 14a9ab3 commit fc2bc07

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
@@ -1754,19 +1754,17 @@ checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
17541754
auto ctor = dyn_cast<ConstructorDecl>(DC);
17551755
if (!ctor || ctor->isConvenienceInit()) {
17561756
TC.diagnose(
1757-
PD->getLoc(),
1758-
diag::inherited_default_value_not_in_designated_constructor);
1757+
PD, diag::inherited_default_value_not_in_designated_constructor);
17591758
return;
17601759
}
17611760

17621761
// The decl it overrides should also be a designated initializer.
17631762
auto overridden = ctor->getOverriddenDecl();
17641763
if (!overridden || overridden->isConvenienceInit()) {
17651764
TC.diagnose(
1766-
PD->getLoc(),
1767-
diag::inherited_default_value_used_in_non_overriding_constructor);
1765+
PD, diag::inherited_default_value_used_in_non_overriding_constructor);
17681766
if (overridden)
1769-
TC.diagnose(overridden->getLoc(), diag::overridden_here);
1767+
TC.diagnose(overridden, diag::overridden_here);
17701768
return;
17711769
}
17721770

@@ -1775,8 +1773,8 @@ checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
17751773
assert(idx && "containing decl does not contain param?");
17761774
ParamDecl *equivalentParam = overridden->getParameters()->get(*idx);
17771775
if (equivalentParam->getDefaultArgumentKind() == DefaultArgumentKind::None) {
1778-
TC.diagnose(PD->getLoc(), diag::corresponding_param_not_defaulted);
1779-
TC.diagnose(equivalentParam->getLoc(), diag::inherited_default_param_here);
1776+
TC.diagnose(PD, diag::corresponding_param_not_defaulted);
1777+
TC.diagnose(equivalentParam, diag::inherited_default_param_here);
17801778
}
17811779
}
17821780

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)