Skip to content

Commit ad82261

Browse files
authored
Merge pull request #80884 from eeckstein/embedded-error-message2
embedded: support default methods in existentials and improve embedded error reporting
2 parents 9cd3787 + 6c31eb0 commit ad82261

File tree

65 files changed

+1158
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1158
-460
lines changed

SwiftCompilerSources/Sources/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_swift_compiler_module(AST
1212
SOURCES
1313
Declarations.swift
1414
Conformance.swift
15+
DiagnosticEngine.swift
1516
GenericSignature.swift
1617
Registration.swift
1718
SubstitutionMap.swift

SwiftCompilerSources/Sources/AST/Conformance.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ASTBridging
1717
/// members to the type (or extension) members that provide the functionality for the concrete type.
1818
///
1919
/// TODO: Ideally, `Conformance` should be an enum
20-
public struct Conformance: CustomStringConvertible, NoReflectionChildren {
20+
public struct Conformance: CustomStringConvertible, Hashable, NoReflectionChildren {
2121
public let bridged: BridgedConformance
2222

2323
public init(bridged: BridgedConformance) {
@@ -28,6 +28,14 @@ public struct Conformance: CustomStringConvertible, NoReflectionChildren {
2828
return String(taking: bridged.getDebugDescription())
2929
}
3030

31+
public func hash(into hasher: inout Hasher) {
32+
hasher.combine(bridged.opaqueValue)
33+
}
34+
35+
public static func ==(lhs: Conformance, rhs: Conformance) -> Bool {
36+
lhs.bridged.opaqueValue == rhs.bridged.opaqueValue
37+
}
38+
3139
public var isConcrete: Bool { bridged.isConcrete() }
3240

3341
public var isValid: Bool { bridged.isValid() }
@@ -37,7 +45,7 @@ public struct Conformance: CustomStringConvertible, NoReflectionChildren {
3745
return Type(bridged: bridged.getType())
3846
}
3947

40-
public var proto: ProtocolDecl {
48+
public var `protocol`: ProtocolDecl {
4149
return bridged.getRequirement().getAs(ProtocolDecl.self)
4250
}
4351
public var isSpecialized: Bool {

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class Decl: CustomStringConvertible, Hashable {
2323
/// The module in which this declaration resides.
2424
public var parentModule: ModuleDecl { bridged.getModuleContext().getAs(ModuleDecl.self) }
2525

26+
/// The parent DeclContext if it is a Decl.
27+
public var parent: Decl? { bridged.getParent().decl }
28+
2629
// True if this declaration is imported from C/C++/ObjC.
2730
public var hasClangNode: Bool { bridged.hasClangNode() }
2831

@@ -69,7 +72,9 @@ final public class ClassDecl: NominalTypeDecl {
6972
}
7073
}
7174

72-
final public class ProtocolDecl: NominalTypeDecl {}
75+
final public class ProtocolDecl: NominalTypeDecl {
76+
public var requiresClass: Bool { bridged.ProtocolDecl_requiresClass() }
77+
}
7378

7479
final public class BuiltinTupleDecl: NominalTypeDecl {}
7580

SwiftCompilerSources/Sources/Optimizer/Utilities/DiagnosticEngine.swift renamed to SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,34 @@
1313
import ASTBridging
1414

1515
import Basic
16-
import SIL
1716

18-
typealias DiagID = BridgedDiagID
17+
public typealias DiagID = BridgedDiagID
1918

20-
protocol DiagnosticArgument {
19+
public protocol DiagnosticArgument {
2120
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
2221
}
2322
extension String: DiagnosticArgument {
24-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
23+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
2524
_withBridgedStringRef { fn(BridgedDiagnosticArgument($0)) }
2625
}
2726
}
2827
extension StringRef: DiagnosticArgument {
29-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
28+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3029
fn(BridgedDiagnosticArgument(_bridged))
3130
}
3231
}
3332
extension Int: DiagnosticArgument {
34-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
33+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3534
fn(BridgedDiagnosticArgument(self))
3635
}
3736
}
3837
extension Type: DiagnosticArgument {
39-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
40-
fn(bridged.asDiagnosticArgument())
41-
}
42-
}
43-
extension DeclRef: DiagnosticArgument {
44-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
38+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
4539
fn(bridged.asDiagnosticArgument())
4640
}
4741
}
4842

49-
struct DiagnosticFixIt {
43+
public struct DiagnosticFixIt {
5044
let start: SourceLoc
5145
let byteLength: Int
5246
let text: String
@@ -67,10 +61,10 @@ struct DiagnosticFixIt {
6761
}
6862
}
6963

70-
struct DiagnosticEngine {
64+
public struct DiagnosticEngine {
7165
private let bridged: BridgedDiagnosticEngine
7266

73-
init(bridged: BridgedDiagnosticEngine) {
67+
public init(bridged: BridgedDiagnosticEngine) {
7468
self.bridged = bridged
7569
}
7670
init?(bridged: BridgedNullableDiagnosticEngine) {
@@ -80,9 +74,9 @@ struct DiagnosticEngine {
8074
self.bridged = BridgedDiagnosticEngine(raw: raw)
8175
}
8276

83-
func diagnose(_ position: SourceLoc?,
84-
_ id: DiagID,
77+
public func diagnose(_ id: DiagID,
8578
_ args: [DiagnosticArgument],
79+
at position: SourceLoc?,
8680
highlight: CharSourceRange? = nil,
8781
fixIts: [DiagnosticFixIt] = []) {
8882

@@ -136,11 +130,32 @@ struct DiagnosticEngine {
136130
closure()
137131
}
138132

139-
func diagnose(_ position: SourceLoc?,
140-
_ id: DiagID,
133+
public func diagnose(_ id: DiagID,
141134
_ args: DiagnosticArgument...,
135+
at position: SourceLoc?,
142136
highlight: CharSourceRange? = nil,
143137
fixIts: DiagnosticFixIt...) {
144-
diagnose(position, id, args, highlight: highlight, fixIts: fixIts)
138+
diagnose(id, args, at: position, highlight: highlight, fixIts: fixIts)
139+
}
140+
141+
public func diagnose(_ diagnostic: Diagnostic) {
142+
diagnose(diagnostic.id, diagnostic.arguments, at: diagnostic.position)
143+
}
144+
}
145+
146+
/// A utility struct which allows throwing a Diagnostic.
147+
public struct Diagnostic : Error {
148+
public let id: DiagID
149+
public let arguments: [DiagnosticArgument]
150+
public let position: SourceLoc?
151+
152+
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at position: SourceLoc?) {
153+
self.init(id, arguments, at: position)
154+
}
155+
156+
public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at position: SourceLoc?) {
157+
self.id = id
158+
self.arguments = arguments
159+
self.position = position
145160
}
146161
}

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
2929
public var genericParameters: TypeArray {
3030
TypeArray(bridged: bridged.getGenericParams())
3131
}
32+
33+
public func mapTypeIntoContext(_ type: Type) -> Type {
34+
Type(bridged: bridged.mapTypeIntoContext(type.bridged))
35+
}
3236
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
4848

4949
public var instanceTypeOfMetatype: Type { Type(bridged: bridged.getInstanceTypeOfMetatype()) }
5050

51+
public var staticTypeOfDynamicSelf: Type { Type(bridged: bridged.getStaticTypeOfDynamicSelf()) }
52+
5153
public var superClassType: Type? {
5254
precondition(isClass)
5355
let bridgedSuperClassTy = bridged.getSuperClassType()
@@ -136,10 +138,12 @@ extension TypeProperties {
136138

137139
public var isTuple: Bool { rawType.bridged.isTuple() }
138140
public var isFunction: Bool { rawType.bridged.isFunction() }
141+
public var isArchetype: Bool { rawType.bridged.isArchetype() }
139142
public var isExistentialArchetype: Bool { rawType.bridged.isExistentialArchetype() }
140143
public var isExistentialArchetypeWithError: Bool { rawType.bridged.isExistentialArchetypeWithError() }
141144
public var isExistential: Bool { rawType.bridged.isExistential() }
142145
public var isClassExistential: Bool { rawType.bridged.isClassExistential() }
146+
public var isGenericTypeParameter: Bool { rawType.bridged.isGenericTypeParam() }
143147
public var isUnownedStorageType: Bool { return rawType.bridged.isUnownedStorageType() }
144148
public var isMetatype: Bool { rawType.bridged.isMetatypeType() }
145149
public var isExistentialMetatype: Bool { rawType.bridged.isExistentialMetatypeType() }
@@ -186,6 +190,7 @@ extension TypeProperties {
186190
public var hasLocalArchetype: Bool { rawType.bridged.hasLocalArchetype() }
187191
public var isEscapable: Bool { rawType.bridged.isEscapable() }
188192
public var isNoEscape: Bool { rawType.bridged.isNoEscape() }
193+
public var archetypeRequiresClass: Bool { rawType.bridged.archetypeRequiresClass() }
189194

190195
public var representationOfMetatype: AST.`Type`.MetatypeRepresentation {
191196
rawType.bridged.getRepresentationOfMetatype().representation

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
9696
return buffer[index]
9797
}
9898

99+
public func startsWith(_ prefix: StaticString) -> Bool {
100+
return prefix.withUTF8Buffer { (prefixBuffer: UnsafeBufferPointer<UInt8>) in
101+
if count < prefixBuffer.count {
102+
return false
103+
}
104+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.data, count: prefixBuffer.count)
105+
return buffer.elementsEqual(prefixBuffer, by: ==)
106+
}
107+
}
108+
99109
public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
100110
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
101111
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DiagnoseInfiniteRecursion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private struct Analysis {
289289
worklist.pushIfNotVisited(function.entryBlock)
290290
while let block = worklist.pop() {
291291
if case .recursive(let apply) = block.getKind(for: invariants, context) {
292-
context.diagnosticEngine.diagnose(apply.location.sourceLoc, .warn_infinite_recursive_call)
292+
context.diagnosticEngine.diagnose(.warn_infinite_recursive_call, at: apply.location)
293293
} else {
294294
for succ in block.successors where isInInfiniteRecursionLoop(succ) {
295295
worklist.pushIfNotVisited(succ)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private struct DiagnoseDependence {
135135

136136
func diagnose(_ position: SourceLoc?, _ id: DiagID,
137137
_ args: DiagnosticArgument...) {
138-
context.diagnosticEngine.diagnose(position, id, args)
138+
context.diagnosticEngine.diagnose(id, args, at: position)
139139
}
140140

141141
/// Check that this use is inside the dependence scope.

SwiftCompilerSources/Sources/Optimizer/ModulePasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
swift_compiler_sources(Optimizer
1010
DiagnoseUnknownConstValues.swift
11+
EmbeddedSwiftDiagnostics.swift
1112
MandatoryPerformanceOptimizations.swift
1213
ReadOnlyGlobalVariables.swift
1314
StackProtection.swift

SwiftCompilerSources/Sources/Optimizer/ModulePasses/DiagnoseUnknownConstValues.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ let diagnoseUnknownConstValues = ModulePass(name: "diagnose-unknown-const-values
3939
private func verifyGlobals(_ context: ModulePassContext) {
4040
for gv in context.globalVariables where gv.isConst {
4141
if gv.staticInitValue == nil {
42-
context.diagnosticEngine.diagnose(gv.varDecl?.location.sourceLoc,
43-
.require_const_initializer_for_const)
42+
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
43+
at: gv.varDecl?.location.sourceLoc)
4444
}
4545
}
4646
}
@@ -66,8 +66,8 @@ private func verifyLocal(debugValueInst: DebugValueInst,
6666
}
6767

6868
if !constExprState.isConstantValue(debugValueInst.operand.value) {
69-
context.diagnosticEngine.diagnose(debugValueInst.location.sourceLoc,
70-
.require_const_initializer_for_const)
69+
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
70+
at: debugValueInst.location)
7171
}
7272
}
7373

@@ -92,8 +92,8 @@ private func verifyCallArguments(apply: FullApplySite,
9292
for (paramIdx, param) in calleeFn.convention.parameters.enumerated() where param.hasOption(.const) {
9393
let matchingOperand = apply.parameterOperands[paramIdx]
9494
if !constExprState.isConstantValue(matchingOperand.value) {
95-
context.diagnosticEngine.diagnose(apply.location.sourceLoc,
96-
.require_const_arg_for_parameter)
95+
context.diagnosticEngine.diagnose(.require_const_arg_for_parameter,
96+
at: apply.location)
9797
}
9898
}
9999
}

0 commit comments

Comments
 (0)