Skip to content

Commit 33ad7cd

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 3570412 + ccaf7e1 commit 33ad7cd

Some content is hidden

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

42 files changed

+540
-219
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,40 @@ public struct DiagnosticEngine {
138138
diagnose(id, args, at: position, highlight: highlight, fixIts: fixIts)
139139
}
140140

141-
public func diagnose(_ diagnostic: Diagnostic) {
142-
diagnose(diagnostic.id, diagnostic.arguments, at: diagnostic.position)
141+
public func diagnose<SourceLocation: ProvidingSourceLocation>(_ diagnostic: Diagnostic<SourceLocation>) {
142+
let loc = diagnostic.location.getSourceLocation(diagnosticEngine: self)
143+
diagnose(diagnostic.id, diagnostic.arguments, at: loc)
143144
}
145+
146+
/// Loads the file at `path` and returns a `SourceLoc` pointing to `line` and `column` in the file.
147+
/// Returns nil if the file cannot be loaded.
148+
public func getLocationFromExternalSource(path: StringRef, line: Int, column: Int) -> SourceLoc? {
149+
return SourceLoc(bridged: bridged.getLocationFromExternalSource(path: path._bridged, line: line, column: column))
150+
}
151+
}
152+
153+
/// Something which can provide a `SourceLoc` for diagnostics.
154+
public protocol ProvidingSourceLocation {
155+
func getSourceLocation(diagnosticEngine: DiagnosticEngine) -> SourceLoc?
156+
}
157+
158+
extension SourceLoc: ProvidingSourceLocation {
159+
public func getSourceLocation(diagnosticEngine: DiagnosticEngine) -> SourceLoc? { self }
144160
}
145161

146162
/// A utility struct which allows throwing a Diagnostic.
147-
public struct Diagnostic : Error {
163+
public struct Diagnostic<SourceLocation: ProvidingSourceLocation> : Error {
148164
public let id: DiagID
149165
public let arguments: [DiagnosticArgument]
150-
public let position: SourceLoc?
166+
public let location: SourceLocation
151167

152-
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at position: SourceLoc?) {
153-
self.init(id, arguments, at: position)
168+
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at location: SourceLocation) {
169+
self.init(id, arguments, at: location)
154170
}
155171

156-
public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at position: SourceLoc?) {
172+
public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at location: SourceLocation) {
157173
self.id = id
158174
self.arguments = arguments
159-
self.position = position
175+
self.location = location
160176
}
161177
}

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313
import BasicBridging
1414

15+
/// Represents a location in source code.
16+
/// It is basically a pointer into a buffer of the loaded source file (managed by `DiagnosticEngine`).
17+
/// In contrast to just having a filename+line+column, this allows displaying the context around
18+
/// the location when printing diagnostics.
1519
public struct SourceLoc {
16-
/// Points into a source file.
1720
public let bridged: BridgedSourceLoc
1821

1922
public init?(bridged: BridgedSourceLoc) {

SwiftCompilerSources/Sources/Optimizer/ModulePasses/EmbeddedSwiftDiagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let embeddedSwiftDiagnostics = ModulePass(name: "embedded-swift-diagnostics") {
3737
do {
3838
assert(checker.callStack.isEmpty)
3939
try checker.checkFunction(function)
40-
} catch let error as Diagnostic {
40+
} catch let error as Diagnostic<Location> {
4141
checker.diagnose(error)
4242
} catch {
4343
fatalError("unknown error thrown")
@@ -248,9 +248,9 @@ private struct FunctionChecker {
248248
}
249249
}
250250

251-
mutating func diagnose(_ error: Diagnostic) {
251+
mutating func diagnose(_ error: Diagnostic<Location>) {
252252
var diagPrinted = false
253-
if error.position != nil {
253+
if error.location.hasValidLineNumber {
254254
context.diagnosticEngine.diagnose(error)
255255
diagPrinted = true
256256
}

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ extension Conformance {
7676

7777
extension DiagnosticEngine {
7878
public func diagnose(_ id: DiagID, _ args: DiagnosticArgument..., at location: Location) {
79-
diagnose(id, args, at: location.sourceLoc)
79+
diagnose(id, args, at: location.getSourceLocation(diagnosticEngine: self))
8080
}
8181

8282
public func diagnose(_ id: DiagID, _ args: [DiagnosticArgument], at location: Location) {
83-
diagnose(id, args, at: location.sourceLoc)
83+
diagnose(id, args, at: location.getSourceLocation(diagnosticEngine: self))
8484
}
8585
}
8686

87-
extension Diagnostic {
87+
extension Diagnostic where SourceLocation == Location {
8888
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at location: Location) {
89-
self.init(id, arguments, at: location.sourceLoc)
89+
self.init(id, arguments, at: location)
9090
}
9191
}

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@
1313
import SILBridging
1414
import AST
1515

16-
public struct Location: Equatable, CustomStringConvertible {
16+
/// Represents a location in source code.
17+
/// `Location` is used in SIL and by the Optimizer.
18+
///
19+
/// When compiling a Swift file, `Location` is basically a `SourceLoc` + information about the
20+
/// containing debug scope. In this case the `SourceLoc` is directly retrieved from the AST nodes.
21+
///
22+
/// However, for debug info which is de-serialized from a swiftmodule file, the location consists of
23+
/// a filename + line and column indices. From such a location, a `SourceLoc` can only be created by
24+
/// loading the file with `DiagnosticEngine.getLocationFromExternalSource`.
25+
///
26+
/// In case of parsing textual SIL (e.g. with `sil-opt`), which does _not_ contain debug line
27+
/// information, the location is also a `SourceLoc` which points to the textual SIL.
28+
public struct Location: ProvidingSourceLocation, Equatable, CustomStringConvertible {
1729
let bridged: BridgedLocation
1830

1931
public var description: String {
@@ -27,6 +39,24 @@ public struct Location: Equatable, CustomStringConvertible {
2739
return nil
2840
}
2941

42+
public var fileNameAndPosition: (path: StringRef, line: Int, column: Int)? {
43+
if bridged.isFilenameAndLocation() {
44+
let loc = bridged.getFilenameAndLocation()
45+
return (StringRef(bridged: loc.path), loc.line, loc.column)
46+
}
47+
return nil
48+
}
49+
50+
public func getSourceLocation(diagnosticEngine: DiagnosticEngine) -> SourceLoc? {
51+
if let sourceLoc = sourceLoc {
52+
return sourceLoc
53+
}
54+
if let (path, line, column) = fileNameAndPosition {
55+
return diagnosticEngine.getLocationFromExternalSource(path: path, line: line, column: column)
56+
}
57+
return nil
58+
}
59+
3060
/// Keeps the debug scope but marks it as auto-generated.
3161
public var autoGenerated: Location {
3262
Location(bridged: bridged.getAutogeneratedLocation())

include/swift/AST/ASTBridging.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,23 @@ BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
215215
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
216216
BridgedSourceLoc cRParenLoc);
217217

218+
SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:moduleSelectorLoc:baseNameLoc:"
219+
"lParenLoc:argumentLabelLocs:rParenLoc:)")
220+
BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
221+
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
222+
BridgedSourceLoc cBaseNameLoc, BridgedSourceLoc cLParenLoc,
223+
BridgedArrayRef cLabelLocs, BridgedSourceLoc cRParenLoc);
224+
218225
SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:)")
219226
BridgedDeclNameLoc
220227
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc);
221228

229+
SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:moduleSelectorLoc:baseNameLoc:)")
230+
BridgedDeclNameLoc
231+
BridgedDeclNameLoc_createParsed(
232+
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
233+
BridgedSourceLoc cBaseNameLoc);
234+
222235
//===----------------------------------------------------------------------===//
223236
// MARK: ASTContext
224237
//===----------------------------------------------------------------------===//
@@ -592,6 +605,11 @@ void BridgedDiagnosticEngine_diagnose(
592605
BridgedArrayRef arguments, BridgedSourceLoc highlightStart,
593606
uint32_t hightlightLength, BridgedArrayRef fixIts);
594607

608+
SWIFT_NAME("BridgedDiagnosticEngine.getLocationFromExternalSource(self:path:line:column:)")
609+
BridgedSourceLoc BridgedDiagnostic_getLocationFromExternalSource(
610+
BridgedDiagnosticEngine bridgedEngine, BridgedStringRef path,
611+
SwiftInt line, SwiftInt column);
612+
595613
SWIFT_NAME("getter:BridgedDiagnosticEngine.hadAnyError(self:)")
596614
bool BridgedDiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
597615

include/swift/AST/DeclNameLoc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "swift/Basic/LLVM.h"
2121
#include "swift/Basic/SourceLoc.h"
2222

23+
#include "llvm/ADT/ArrayRef.h"
24+
2325
class BridgedDeclNameLoc;
2426

2527
namespace swift {
@@ -71,13 +73,24 @@ class DeclNameLoc {
7173
explicit DeclNameLoc(SourceLoc baseNameLoc)
7274
: DeclNameLoc(baseNameLoc.getOpaquePointerValue(), 0) {}
7375

76+
explicit DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
77+
SourceLoc baseNameLoc)
78+
: DeclNameLoc(baseNameLoc) { }
79+
7480
/// Create declaration name location information for a compound
7581
/// name.
7682
DeclNameLoc(ASTContext &ctx, SourceLoc baseNameLoc,
7783
SourceLoc lParenLoc,
7884
ArrayRef<SourceLoc> argumentLabelLocs,
7985
SourceLoc rParenLoc);
8086

87+
DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
88+
SourceLoc baseNameLoc,
89+
SourceLoc lParenLoc,
90+
ArrayRef<SourceLoc> argumentLabelLocs,
91+
SourceLoc rParenLoc)
92+
: DeclNameLoc(ctx, baseNameLoc, lParenLoc, argumentLabelLocs, rParenLoc) { }
93+
8194
/// Whether the location information is valid.
8295
bool isValid() const { return getBaseNameLoc().isValid(); }
8396

@@ -111,6 +124,10 @@ class DeclNameLoc {
111124
return getSourceLocs()[FirstArgumentLabelIndex + index];
112125
}
113126

127+
SourceLoc getModuleSelectorLoc() const {
128+
return SourceLoc();
129+
}
130+
114131
SourceLoc getStartLoc() const {
115132
return getBaseNameLoc();
116133
}

include/swift/AST/Identifier.h

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -693,65 +693,73 @@ class DeclNameRef {
693693
void *getOpaqueValue() const { return FullName.getOpaqueValue(); }
694694
static DeclNameRef getFromOpaqueValue(void *p);
695695

696+
explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
697+
DeclName fullName)
698+
: FullName(fullName) { }
699+
700+
explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
701+
DeclBaseName baseName, ArrayRef<Identifier> argLabels)
702+
: FullName(C, baseName, argLabels) { }
703+
696704
explicit DeclNameRef(DeclName FullName)
697705
: FullName(FullName) { }
698706

699-
explicit DeclNameRef(DeclBaseName BaseName)
700-
: FullName(BaseName) { }
707+
bool hasModuleSelector() const {
708+
return false;
709+
}
701710

702-
explicit DeclNameRef(Identifier BaseName)
703-
: FullName(BaseName) { }
711+
Identifier getModuleSelector() const {
712+
return Identifier();
713+
}
704714

705715
/// The name of the declaration being referenced.
706716
DeclName getFullName() const {
707717
return FullName;
708718
}
709719

710-
DeclName &getFullName() {
711-
return FullName;
712-
}
713-
714720
/// The base name of the declaration being referenced.
715721
DeclBaseName getBaseName() const {
716-
return FullName.getBaseName();
722+
return getFullName().getBaseName();
717723
}
718724

719725
Identifier getBaseIdentifier() const {
720-
return FullName.getBaseIdentifier();
726+
return getFullName().getBaseIdentifier();
721727
}
722728

723729
ArrayRef<Identifier> getArgumentNames() const {
724-
return FullName.getArgumentNames();
730+
return getFullName().getArgumentNames();
725731
}
726732

727733
bool isSimpleName() const {
728-
return FullName.isSimpleName();
734+
return getFullName().isSimpleName();
729735
}
730736

731737
bool isSimpleName(DeclBaseName name) const {
732-
return FullName.isSimpleName(name);
738+
return getFullName().isSimpleName(name);
733739
}
734740

735741
bool isSimpleName(StringRef name) const {
736-
return FullName.isSimpleName(name);
742+
return getFullName().isSimpleName(name);
737743
}
738744

739745
bool isSpecial() const {
740-
return FullName.isSpecial();
746+
return getFullName().isSpecial();
741747
}
742748

743749
bool isOperator() const {
744-
return FullName.isOperator();
750+
return getFullName().isOperator();
745751
}
746752

747-
bool mustAlwaysBeEscaped() const { return FullName.mustAlwaysBeEscaped(); }
753+
bool mustAlwaysBeEscaped() const {
754+
return getFullName().mustAlwaysBeEscaped();
755+
}
748756

749757
bool isCompoundName() const {
750-
return FullName.isCompoundName();
758+
return getFullName().isCompoundName();
751759
}
752760

753761
explicit operator bool() const {
754-
return (bool)FullName;
762+
return (bool)getFullName();
755763
}
756764

757765
/// Compare two declaration names, producing -1 if \c *this comes before
@@ -791,7 +799,7 @@ class DeclNameRef {
791799
return lhs.compare(rhs) >= 0;
792800
}
793801

794-
DeclNameRef withoutArgumentLabels() const;
802+
DeclNameRef withoutArgumentLabels(ASTContext &C) const;
795803
DeclNameRef withArgumentLabels(ASTContext &C,
796804
ArrayRef<Identifier> argumentNames) const;
797805

@@ -824,16 +832,15 @@ inline DeclNameRef DeclNameRef::getFromOpaqueValue(void *p) {
824832
return DeclNameRef(DeclName::getFromOpaqueValue(p));
825833
}
826834

827-
inline DeclNameRef DeclNameRef::withoutArgumentLabels() const {
828-
return DeclNameRef(getBaseName());
835+
inline DeclNameRef DeclNameRef::withoutArgumentLabels(ASTContext &C) const {
836+
return DeclNameRef(C, getModuleSelector(), getBaseName());
829837
}
830838

831839
inline DeclNameRef DeclNameRef::withArgumentLabels(
832840
ASTContext &C, ArrayRef<Identifier> argumentNames) const {
833-
return DeclNameRef(DeclName(C, getBaseName(), argumentNames));
841+
return DeclNameRef(C, getModuleSelector(), getBaseName(), argumentNames);
834842
}
835843

836-
837844
inline DeclNameRef DeclNameRef::createSubscript() {
838845
return DeclNameRef(DeclBaseName::createSubscript());
839846
}

include/swift/AST/ProtocolConformance.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,7 @@ class SelfProtocolConformance : public RootProtocolConformance {
918918
}
919919

920920
ProtocolConformanceRef getAssociatedConformance(Type assocType,
921-
ProtocolDecl *protocol) const{
922-
llvm_unreachable("self-conformances never have associated types");
923-
}
921+
ProtocolDecl *protocol) const;
924922

925923
bool hasWitness(ValueDecl *requirement) const {
926924
return true;

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ EXPERIMENTAL_FEATURE(CDecl, false)
508508
/// Allow use of `@extensible` on public enums
509509
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)
510510

511+
/// Allow use of `Module::name` syntax
512+
EXPERIMENTAL_FEATURE(ModuleSelector, false)
513+
511514
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
512515
#undef EXPERIMENTAL_FEATURE
513516
#undef UPCOMING_FEATURE

0 commit comments

Comments
 (0)