Skip to content

embedded: support default methods in existentials and improve embedded error reporting #80884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8e874cd
SIL: add better support for specialized witness tables.
eeckstein Apr 17, 2025
43d793b
GenericSpecializer: fix a bug which prevented specialization for type…
eeckstein Apr 17, 2025
1c9a7cd
SwiftCompilerSources: refactor DiagnosticEngine
eeckstein Apr 17, 2025
75152f2
Swift AST: make `Conformance` hashable and rename `var proto` -> `var…
eeckstein Apr 17, 2025
2020897
Swift AST: add some Decl APIs
eeckstein Apr 17, 2025
9aa61e1
Swift AST: add some `Type` APIs
eeckstein Apr 17, 2025
fd17b7e
Swift AST: add `GenericSignature.mapTypeIntoContext`
eeckstein Apr 17, 2025
d1f581b
Swift Utils: add `StringRef.startsWith`
eeckstein Apr 17, 2025
f0f1c3c
SIL: make `SILFunctionConvention` constructible without needing a SIL…
eeckstein Apr 17, 2025
e154e12
Swift SIL: add APIs for box types
eeckstein Apr 17, 2025
dbf1bf0
Swift SIL: add a comment for `VTable.specializedClassType`
eeckstein Apr 17, 2025
5a67af5
Swift SIL: add `Function.isSpecialization`
eeckstein Apr 17, 2025
cab6232
Swift SIL: improvements for `Location`
eeckstein Apr 17, 2025
655dae8
Optimizer: add `Options.noAllocations`
eeckstein Apr 17, 2025
f7d81f7
Swift SIL: deal with a generic self type in `SubstitutionMap.getMetho…
eeckstein Apr 17, 2025
7a8a50a
Swift SIL: add `Type.hasValidSignatureForEmbedded`
eeckstein Apr 17, 2025
d222cf2
MandatoryPerformanceOptimizations: support default methods for class …
eeckstein Apr 17, 2025
6c31eb0
embedded: rewrite the diagnostic pass for embedded swift
eeckstein Apr 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_swift_compiler_module(AST
SOURCES
Declarations.swift
Conformance.swift
DiagnosticEngine.swift
GenericSignature.swift
Registration.swift
SubstitutionMap.swift
Expand Down
12 changes: 10 additions & 2 deletions SwiftCompilerSources/Sources/AST/Conformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ASTBridging
/// members to the type (or extension) members that provide the functionality for the concrete type.
///
/// TODO: Ideally, `Conformance` should be an enum
public struct Conformance: CustomStringConvertible, NoReflectionChildren {
public struct Conformance: CustomStringConvertible, Hashable, NoReflectionChildren {
public let bridged: BridgedConformance

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

public func hash(into hasher: inout Hasher) {
hasher.combine(bridged.opaqueValue)
}

public static func ==(lhs: Conformance, rhs: Conformance) -> Bool {
lhs.bridged.opaqueValue == rhs.bridged.opaqueValue
}

public var isConcrete: Bool { bridged.isConcrete() }

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

public var proto: ProtocolDecl {
public var `protocol`: ProtocolDecl {
return bridged.getRequirement().getAs(ProtocolDecl.self)
}
public var isSpecialized: Bool {
Expand Down
7 changes: 6 additions & 1 deletion SwiftCompilerSources/Sources/AST/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class Decl: CustomStringConvertible, Hashable {
/// The module in which this declaration resides.
public var parentModule: ModuleDecl { bridged.getModuleContext().getAs(ModuleDecl.self) }

/// The parent DeclContext if it is a Decl.
public var parent: Decl? { bridged.getParent().decl }

// True if this declaration is imported from C/C++/ObjC.
public var hasClangNode: Bool { bridged.hasClangNode() }

Expand Down Expand Up @@ -69,7 +72,9 @@ final public class ClassDecl: NominalTypeDecl {
}
}

final public class ProtocolDecl: NominalTypeDecl {}
final public class ProtocolDecl: NominalTypeDecl {
public var requiresClass: Bool { bridged.ProtocolDecl_requiresClass() }
}

final public class BuiltinTupleDecl: NominalTypeDecl {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,34 @@
import ASTBridging

import Basic
import SIL

typealias DiagID = BridgedDiagID
public typealias DiagID = BridgedDiagID

protocol DiagnosticArgument {
public protocol DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
}
extension String: DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
_withBridgedStringRef { fn(BridgedDiagnosticArgument($0)) }
}
}
extension StringRef: DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(BridgedDiagnosticArgument(_bridged))
}
}
extension Int: DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(BridgedDiagnosticArgument(self))
}
}
extension Type: DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(bridged.asDiagnosticArgument())
}
}
extension DeclRef: DiagnosticArgument {
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(bridged.asDiagnosticArgument())
}
}

struct DiagnosticFixIt {
public struct DiagnosticFixIt {
let start: SourceLoc
let byteLength: Int
let text: String
Expand All @@ -67,10 +61,10 @@ struct DiagnosticFixIt {
}
}

struct DiagnosticEngine {
public struct DiagnosticEngine {
private let bridged: BridgedDiagnosticEngine

init(bridged: BridgedDiagnosticEngine) {
public init(bridged: BridgedDiagnosticEngine) {
self.bridged = bridged
}
init?(bridged: BridgedNullableDiagnosticEngine) {
Expand All @@ -80,9 +74,9 @@ struct DiagnosticEngine {
self.bridged = BridgedDiagnosticEngine(raw: raw)
}

func diagnose(_ position: SourceLoc?,
_ id: DiagID,
public func diagnose(_ id: DiagID,
_ args: [DiagnosticArgument],
at position: SourceLoc?,
highlight: CharSourceRange? = nil,
fixIts: [DiagnosticFixIt] = []) {

Expand Down Expand Up @@ -136,11 +130,32 @@ struct DiagnosticEngine {
closure()
}

func diagnose(_ position: SourceLoc?,
_ id: DiagID,
public func diagnose(_ id: DiagID,
_ args: DiagnosticArgument...,
at position: SourceLoc?,
highlight: CharSourceRange? = nil,
fixIts: DiagnosticFixIt...) {
diagnose(position, id, args, highlight: highlight, fixIts: fixIts)
diagnose(id, args, at: position, highlight: highlight, fixIts: fixIts)
}

public func diagnose(_ diagnostic: Diagnostic) {
diagnose(diagnostic.id, diagnostic.arguments, at: diagnostic.position)
}
}

/// A utility struct which allows throwing a Diagnostic.
public struct Diagnostic : Error {
public let id: DiagID
public let arguments: [DiagnosticArgument]
public let position: SourceLoc?

public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at position: SourceLoc?) {
self.init(id, arguments, at: position)
}

public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at position: SourceLoc?) {
self.id = id
self.arguments = arguments
self.position = position
}
}
4 changes: 4 additions & 0 deletions SwiftCompilerSources/Sources/AST/GenericSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
public var genericParameters: TypeArray {
TypeArray(bridged: bridged.getGenericParams())
}

public func mapTypeIntoContext(_ type: Type) -> Type {
Type(bridged: bridged.mapTypeIntoContext(type.bridged))
}
}
5 changes: 5 additions & 0 deletions SwiftCompilerSources/Sources/AST/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre

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

public var staticTypeOfDynamicSelf: Type { Type(bridged: bridged.getStaticTypeOfDynamicSelf()) }

public var superClassType: Type? {
precondition(isClass)
let bridgedSuperClassTy = bridged.getSuperClassType()
Expand Down Expand Up @@ -136,10 +138,12 @@ extension TypeProperties {

public var isTuple: Bool { rawType.bridged.isTuple() }
public var isFunction: Bool { rawType.bridged.isFunction() }
public var isArchetype: Bool { rawType.bridged.isArchetype() }
public var isExistentialArchetype: Bool { rawType.bridged.isExistentialArchetype() }
public var isExistentialArchetypeWithError: Bool { rawType.bridged.isExistentialArchetypeWithError() }
public var isExistential: Bool { rawType.bridged.isExistential() }
public var isClassExistential: Bool { rawType.bridged.isClassExistential() }
public var isGenericTypeParameter: Bool { rawType.bridged.isGenericTypeParam() }
public var isUnownedStorageType: Bool { return rawType.bridged.isUnownedStorageType() }
public var isMetatype: Bool { rawType.bridged.isMetatypeType() }
public var isExistentialMetatype: Bool { rawType.bridged.isExistentialMetatypeType() }
Expand Down Expand Up @@ -186,6 +190,7 @@ extension TypeProperties {
public var hasLocalArchetype: Bool { rawType.bridged.hasLocalArchetype() }
public var isEscapable: Bool { rawType.bridged.isEscapable() }
public var isNoEscape: Bool { rawType.bridged.isNoEscape() }
public var archetypeRequiresClass: Bool { rawType.bridged.archetypeRequiresClass() }

public var representationOfMetatype: AST.`Type`.MetatypeRepresentation {
rawType.bridged.getRepresentationOfMetatype().representation
Expand Down
10 changes: 10 additions & 0 deletions SwiftCompilerSources/Sources/Basic/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
return buffer[index]
}

public func startsWith(_ prefix: StaticString) -> Bool {
return prefix.withUTF8Buffer { (prefixBuffer: UnsafeBufferPointer<UInt8>) in
if count < prefixBuffer.count {
return false
}
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.data, count: prefixBuffer.count)
return buffer.elementsEqual(prefixBuffer, by: ==)
}
}

public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private struct Analysis {
worklist.pushIfNotVisited(function.entryBlock)
while let block = worklist.pop() {
if case .recursive(let apply) = block.getKind(for: invariants, context) {
context.diagnosticEngine.diagnose(apply.location.sourceLoc, .warn_infinite_recursive_call)
context.diagnosticEngine.diagnose(.warn_infinite_recursive_call, at: apply.location)
} else {
for succ in block.successors where isInInfiniteRecursionLoop(succ) {
worklist.pushIfNotVisited(succ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private struct DiagnoseDependence {

func diagnose(_ position: SourceLoc?, _ id: DiagID,
_ args: DiagnosticArgument...) {
context.diagnosticEngine.diagnose(position, id, args)
context.diagnosticEngine.diagnose(id, args, at: position)
}

/// Check that this use is inside the dependence scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

swift_compiler_sources(Optimizer
DiagnoseUnknownConstValues.swift
EmbeddedSwiftDiagnostics.swift
MandatoryPerformanceOptimizations.swift
ReadOnlyGlobalVariables.swift
StackProtection.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ let diagnoseUnknownConstValues = ModulePass(name: "diagnose-unknown-const-values
private func verifyGlobals(_ context: ModulePassContext) {
for gv in context.globalVariables where gv.isConst {
if gv.staticInitValue == nil {
context.diagnosticEngine.diagnose(gv.varDecl?.location.sourceLoc,
.require_const_initializer_for_const)
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
at: gv.varDecl?.location.sourceLoc)
}
}
}
Expand All @@ -66,8 +66,8 @@ private func verifyLocal(debugValueInst: DebugValueInst,
}

if !constExprState.isConstantValue(debugValueInst.operand.value) {
context.diagnosticEngine.diagnose(debugValueInst.location.sourceLoc,
.require_const_initializer_for_const)
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
at: debugValueInst.location)
}
}

Expand All @@ -92,8 +92,8 @@ private func verifyCallArguments(apply: FullApplySite,
for (paramIdx, param) in calleeFn.convention.parameters.enumerated() where param.hasOption(.const) {
let matchingOperand = apply.parameterOperands[paramIdx]
if !constExprState.isConstantValue(matchingOperand.value) {
context.diagnosticEngine.diagnose(apply.location.sourceLoc,
.require_const_arg_for_parameter)
context.diagnosticEngine.diagnose(.require_const_arg_for_parameter,
at: apply.location)
}
}
}
Expand Down
Loading