Skip to content

Commit dedf920

Browse files
Merge pull request #4914 from swiftwasm/main
[pull] swiftwasm from main
2 parents 8e2626e + 81c3a11 commit dedf920

34 files changed

+286
-76
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,14 @@ endif()
877877

878878
# When we have the early SwiftSyntax build, we can include its parser.
879879
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
880-
set(SWIFT_SWIFT_PARSER TRUE)
881-
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
880+
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
881+
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
882+
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
883+
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
884+
else()
885+
set(SWIFT_SWIFT_PARSER TRUE)
886+
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
887+
endif()
882888
endif()
883889

884890

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function(add_swift_unittest test_dirname)
8181

8282
if (SWIFT_SWIFT_PARSER)
8383
_add_swift_runtime_link_flags(${test_dirname} "../../lib" "")
84+
set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF)
8485
endif()
8586
endfunction()
8687

include/swift/AST/Decl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,12 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
10491049

10501050
bool isAvailableAsSPI() const;
10511051

1052+
/// Whether the declaration is considered unavailable through either being
1053+
/// explicitly marked as such, or has a parent decl that is semantically
1054+
/// unavailable. This is a broader notion of unavailability than is checked by
1055+
/// \c AvailableAttr::isUnavailable.
1056+
bool isSemanticallyUnavailable() const;
1057+
10521058
// List the SPI groups declared with @_spi or inherited by this decl.
10531059
//
10541060
// SPI groups are inherited from the parent contexts only if the local decl

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,8 +3014,8 @@ NOTE(spi_only_import_conflict_here,none,
30143014
"imported for SPI only here", ())
30153015

30163016
ERROR(error_public_import_of_private_module,none,
3017-
"private module %0 is imported publicly from the public module %1",
3018-
(Identifier, Identifier))
3017+
"private module %0 is imported publicly from the public module %1",
3018+
(Identifier, Identifier))
30193019

30203020
ERROR(implementation_only_decl_non_override,none,
30213021
"'@_implementationOnly' can only be used on overrides", ())

include/swift/AST/TypeCheckRequests.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,6 +3489,22 @@ class RenamedDeclRequest
34893489
bool isCached() const { return true; }
34903490
};
34913491

3492+
class IsSemanticallyUnavailableRequest
3493+
: public SimpleRequest<IsSemanticallyUnavailableRequest,
3494+
bool(const Decl *),
3495+
RequestFlags::Cached> {
3496+
public:
3497+
using SimpleRequest::SimpleRequest;
3498+
3499+
private:
3500+
friend SimpleRequest;
3501+
3502+
bool evaluate(Evaluator &evaluator, const Decl *decl) const;
3503+
3504+
public:
3505+
bool isCached() const { return true; }
3506+
};
3507+
34923508
class ClosureEffectsRequest
34933509
: public SimpleRequest<ClosureEffectsRequest,
34943510
FunctionType::ExtInfo(ClosureExpr *),

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
395395
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
396396
ValueDecl *(const ValueDecl *, const AvailableAttr *),
397397
Cached, NoLocationInfo)
398+
SWIFT_REQUEST(TypeChecker, IsSemanticallyUnavailableRequest,
399+
bool(const Decl *),
400+
Cached, NoLocationInfo)
398401
SWIFT_REQUEST(TypeChecker, ClosureEffectsRequest,
399402
FunctionType::ExtInfo(ClosureExpr *),
400403
Cached, NoLocationInfo)

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ EXPERIMENTAL_FEATURE(ParserRoundTrip)
134134
/// Swift parser.
135135
EXPERIMENTAL_FEATURE(ParserValidation)
136136

137+
/// Whether to fold sequence expressions in the syntax tree produced by the
138+
/// Swift Swift parser.
139+
EXPERIMENTAL_FEATURE(ParserSequenceFolding)
140+
137141
#undef EXPERIMENTAL_FEATURE
138142
#undef UPCOMING_FEATURE
139143
#undef SUPPRESSIBLE_LANGUAGE_FEATURE

include/swift/SIL/SILDeclRef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ struct SILDeclRef {
300300
/// Retrieves the ASTContext from the underlying AST node being stored.
301301
ASTContext &getASTContext() const;
302302

303+
/// Retrieve the innermost declaration context corresponding to the underlying
304+
/// node, which will either be the node itself (if it's also a declaration
305+
/// context) or its parent context.
306+
DeclContext *getInnermostDeclContext() const;
307+
303308
llvm::Optional<AnyFunctionRef> getAnyFunctionRef() const;
304309

305310
SILLocation getAsRegularLocation() const;

include/swift/SIL/SILProfiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SILFunction;
3232
class SILModule;
3333

3434
/// Returns whether the given AST node requires profiling instrumentation.
35-
bool doesASTRequireProfiling(SILModule &M, ASTNode N);
35+
bool doesASTRequireProfiling(SILModule &M, ASTNode N, SILDeclRef Constant);
3636

3737
/// SILProfiler - Maps AST nodes to profile counters.
3838
class SILProfiler : public SILAllocated<SILProfiler> {

include/swift/Sema/CompletionContextFinder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class CompletionContextFinder : public ASTWalker {
9191
return CompletionNode.dyn_cast<const KeyPathExpr *>() != nullptr;
9292
}
9393

94+
bool hasCompletion() const {
95+
return !CompletionNode.isNull();
96+
}
97+
9498
/// If we are completing in a key path, returns the \c KeyPath that contains
9599
/// the code completion component.
96100
const KeyPathExpr *getKeyPathContainingCompletionComponent() const {

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,10 @@ static bool usesFeatureParserValidation(Decl *decl) {
29452945
return false;
29462946
}
29472947

2948+
static bool usesFeatureParserSequenceFolding(Decl *decl) {
2949+
return false;
2950+
}
2951+
29482952
static void suppressingFeatureSpecializeAttributeWithAvailability(
29492953
PrintOptions &options,
29502954
llvm::function_ref<void()> action) {

lib/AST/Availability.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Types.h"
2121
#include "swift/AST/Availability.h"
2222
#include "swift/AST/PlatformKind.h"
23+
#include "swift/AST/TypeCheckRequests.h"
2324
#include "swift/AST/TypeWalker.h"
2425
#include <map>
2526

@@ -194,6 +195,41 @@ bool Decl::isAvailableAsSPI() const {
194195
.isAvailableAsSPI();
195196
}
196197

198+
bool IsSemanticallyUnavailableRequest::evaluate(Evaluator &evaluator,
199+
const Decl *decl) const {
200+
// Directly marked unavailable.
201+
if (AvailableAttr::isUnavailable(decl))
202+
return true;
203+
204+
// If this is an extension, it's semantically unavailable if its nominal is,
205+
// as there is no way to reference or construct the type.
206+
if (auto *ext = dyn_cast<ExtensionDecl>(decl)) {
207+
if (auto *nom = ext->getExtendedNominal()) {
208+
if (nom->isSemanticallyUnavailable())
209+
return true;
210+
}
211+
}
212+
213+
// If the parent decl is semantically unavailable, then this decl is too.
214+
// For local contexts, this means it's a local decl in e.g an unavailable
215+
// function, which cannot be accessed. For non-local contexts, this is a
216+
// nested type or a member with an unavailable parent, which cannot be
217+
// referenced.
218+
// Similar to `AvailableAttr::isUnavailable`, don't apply this logic to
219+
// Clang decls, as they may be inaccurately parented.
220+
if (!decl->hasClangNode()) {
221+
auto *DC = decl->getDeclContext();
222+
if (auto *parentDecl = DC->getInnermostDeclarationDeclContext())
223+
return parentDecl->isSemanticallyUnavailable();
224+
}
225+
return false;
226+
}
227+
228+
bool Decl::isSemanticallyUnavailable() const {
229+
auto &eval = getASTContext().evaluator;
230+
return evaluateOrDefault(eval, IsSemanticallyUnavailableRequest{this}, false);
231+
}
232+
197233
AvailabilityContext
198234
AvailabilityInference::annotatedAvailableRangeForAttr(const SpecializeAttr* attr,
199235
ASTContext &ctx) {

lib/Parse/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ if (SWIFT_SWIFT_PARSER)
4444
#
4545
# target_link_libraries(swiftParse
4646
# PRIVATE
47-
# SwiftSyntax::SwiftParser
47+
# SwiftSyntax::SwiftCompilerSupport
4848
# )
4949
target_link_libraries(swiftParse
5050
PRIVATE
5151
$<TARGET_OBJECTS:SwiftSyntax::SwiftParser>
5252
$<TARGET_OBJECTS:SwiftSyntax::SwiftDiagnostics>
5353
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntax>
54+
$<TARGET_OBJECTS:SwiftSyntax::SwiftOperators>
55+
$<TARGET_OBJECTS:SwiftSyntax::SwiftCompilerSupport>
5456
)
5557

5658
target_include_directories(swiftParse
5759
PRIVATE
58-
${CMAKE_CURRENT_SOURCE_DIR}/../../../swift-syntax/Sources/SwiftParser
60+
${CMAKE_CURRENT_SOURCE_DIR}/../../../swift-syntax/Sources/SwiftCompilerSupport
5961
)
6062

6163
target_compile_definitions(swiftParse

lib/Parse/ParseRequests.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "swift/SyntaxParse/SyntaxTreeCreator.h"
2828

2929
#ifdef SWIFT_SWIFT_PARSER
30-
#include "SwiftParserCompilerSupport.h"
30+
#include "SwiftCompilerSupport.h"
3131
#endif
3232

3333
using namespace swift;
@@ -202,11 +202,14 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
202202
unsigned int flags = 0;
203203

204204
if (ctx.LangOpts.hasFeature(Feature::ParserRoundTrip))
205-
flags |= SPCC_RoundTrip;
205+
flags |= SCC_RoundTrip;
206206

207207
if (!ctx.Diags.hadAnyError() &&
208208
ctx.LangOpts.hasFeature(Feature::ParserValidation))
209-
flags |= SPCC_ParseDiagnostics;
209+
flags |= SCC_ParseDiagnostics;
210+
211+
if (ctx.LangOpts.hasFeature(Feature::ParserSequenceFolding))
212+
flags |= SCC_FoldSequences;
210213

211214
int roundTripResult =
212215
swift_parser_consistencyCheck(

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,26 @@ Optional<AnyFunctionRef> SILDeclRef::getAnyFunctionRef() const {
195195
llvm_unreachable("Unhandled case in switch");
196196
}
197197

198-
ASTContext &SILDeclRef::getASTContext() const {
198+
DeclContext *SILDeclRef::getInnermostDeclContext() const {
199+
if (!loc)
200+
return nullptr;
199201
switch (getLocKind()) {
200202
case LocKind::Decl:
201-
return getDecl()->getASTContext();
203+
return getDecl()->getInnermostDeclContext();
202204
case LocKind::Closure:
203-
return getAbstractClosureExpr()->getASTContext();
205+
return getAbstractClosureExpr();
204206
case LocKind::File:
205-
return getFileUnit()->getASTContext();
207+
return getFileUnit();
206208
}
207209
llvm_unreachable("Unhandled case in switch");
208210
}
209211

212+
ASTContext &SILDeclRef::getASTContext() const {
213+
auto *DC = getInnermostDeclContext();
214+
assert(DC && "Must have a decl context");
215+
return DC->getASTContext();
216+
}
217+
210218
Optional<AvailabilityContext> SILDeclRef::getAvailabilityForLinkage() const {
211219
// Back deployment thunks and fallbacks don't have availability since they
212220
// are non-ABI.

lib/SIL/IR/SILProfiler.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,30 @@ static bool doesClosureHaveBody(AbstractClosureExpr *ACE) {
4242
}
4343

4444
/// Check whether a root AST node should be profiled.
45-
static bool shouldProfile(ASTNode N) {
45+
static bool shouldProfile(ASTNode N, SILDeclRef Constant) {
4646
// Do not profile AST nodes with invalid source locations.
4747
if (N.getStartLoc().isInvalid() || N.getEndLoc().isInvalid()) {
4848
LLVM_DEBUG(llvm::dbgs()
4949
<< "Skipping ASTNode: invalid start/end locations\n");
5050
return false;
5151
}
52+
if (!Constant) {
53+
// We should only ever have a null SILDeclRef for top-level code, which is
54+
// always user-written, and should always be profiled.
55+
// FIXME: Once top-level code is unified under a single SILProfiler, this
56+
// case can be eliminated.
57+
assert(isa<TopLevelCodeDecl>(N.get<Decl *>()));
58+
return true;
59+
}
60+
61+
// Do not profile AST nodes in unavailable contexts.
62+
auto *DC = Constant.getInnermostDeclContext();
63+
if (auto *D = DC->getInnermostDeclarationDeclContext()) {
64+
if (D->isSemanticallyUnavailable()) {
65+
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: unavailable context\n");
66+
return false;
67+
}
68+
}
5269

5370
if (auto *E = N.dyn_cast<Expr *>()) {
5471
if (auto *CE = dyn_cast<AbstractClosureExpr>(E)) {
@@ -96,13 +113,13 @@ static bool shouldProfile(ASTNode N) {
96113
}
97114

98115
namespace swift {
99-
bool doesASTRequireProfiling(SILModule &M, ASTNode N) {
116+
bool doesASTRequireProfiling(SILModule &M, ASTNode N, SILDeclRef Constant) {
100117
// If profiling isn't enabled, don't profile anything.
101118
auto &Opts = M.getOptions();
102119
if (Opts.UseProfile.empty() && !Opts.GenerateProfile)
103120
return false;
104121

105-
return shouldProfile(N);
122+
return shouldProfile(N, Constant);
106123
}
107124
} // namespace swift
108125

@@ -157,7 +174,7 @@ static bool canCreateProfilerForAST(ASTNode N, SILDeclRef forDecl) {
157174

158175
SILProfiler *SILProfiler::create(SILModule &M, ASTNode N, SILDeclRef Ref) {
159176
const auto &Opts = M.getOptions();
160-
if (!doesASTRequireProfiling(M, N))
177+
if (!doesASTRequireProfiling(M, N, Ref))
161178
return nullptr;
162179

163180
if (!canCreateProfilerForAST(N, Ref)) {

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ void SILGenModule::emitFunction(FuncDecl *fd) {
14361436

14371437
if (fd->hasBody()) {
14381438
SILDeclRef constant(decl);
1439-
bool ForCoverageMapping = doesASTRequireProfiling(M, fd);
1439+
bool ForCoverageMapping = doesASTRequireProfiling(M, fd, constant);
14401440
emitOrDelayFunction(*this, constant,
14411441
/*forceEmission=*/ForCoverageMapping);
14421442
}

lib/Sema/BuilderTransform.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,9 +2361,11 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
23612361
cs.solveForCodeCompletion(solutions);
23622362

23632363
CompletionContextFinder analyzer(func, func->getDeclContext());
2364-
filterSolutionsForCodeCompletion(solutions, analyzer);
2365-
for (const auto &solution : solutions) {
2366-
cs.getASTContext().CompletionCallback->sawSolution(solution);
2364+
if (analyzer.hasCompletion()) {
2365+
filterSolutionsForCodeCompletion(solutions, analyzer);
2366+
for (const auto &solution : solutions) {
2367+
cs.getASTContext().CompletionCallback->sawSolution(solution);
2368+
}
23672369
}
23682370
return nullptr;
23692371
}

lib/Sema/CompletionContextFinder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ CompletionContextFinder::walkToExprPost(Expr *E) {
7171
}
7272

7373
size_t CompletionContextFinder::getKeyPathCompletionComponentIndex() const {
74-
assert(hasCompletionKeyPathComponent());
7574
size_t ComponentIndex = 0;
7675
auto Components = getKeyPathContainingCompletionComponent()->getComponents();
7776
for (auto &Component : Components) {

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ static bool hasTypeForCompletion(Solution &solution,
518518
if (contextAnalyzer.hasCompletionExpr()) {
519519
return solution.hasType(contextAnalyzer.getCompletionExpr());
520520
} else {
521-
assert(contextAnalyzer.hasCompletionKeyPathComponent());
522521
return solution.hasType(
523522
contextAnalyzer.getKeyPathContainingCompletionComponent(),
524523
contextAnalyzer.getKeyPathCompletionComponentIndex());
@@ -578,8 +577,7 @@ bool TypeChecker::typeCheckForCodeCompletion(
578577

579578
// If there was no completion expr (e.g. if the code completion location was
580579
// among tokens that were skipped over during parser error recovery) bail.
581-
if (!contextAnalyzer.hasCompletionExpr() &&
582-
!contextAnalyzer.hasCompletionKeyPathComponent())
580+
if (!contextAnalyzer.hasCompletion())
583581
return false;
584582

585583
// Interpolation components are type-checked separately.

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,8 +1900,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
19001900
#endif
19011901

19021902
bool isImportOfUnderlying = importer->getName() == target->getName();
1903+
auto *SF = ID->getDeclContext()->getParentSourceFile();
19031904
bool treatAsError = enableTreatAsError &&
1904-
!isImportOfUnderlying;
1905+
!isImportOfUnderlying &&
1906+
SF->Kind != SourceFileKind::Interface;
19051907
if (!treatAsError)
19061908
inFlight.limitBehavior(DiagnosticBehavior::Warning);
19071909
}

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ function(get_test_dependencies SDK result_var_name)
4343
list(APPEND deps SwiftUnitTests)
4444
endif()
4545

46+
if(SWIFT_BUILD_SDK_OVERLAY OR SWIFT_BUILD_TEST_SUPPORT_MODULES)
47+
list(APPEND deps sdk-overlay)
48+
endif()
49+
4650
set(deps_binaries)
4751

4852
if (SWIFT_INCLUDE_TOOLS)

0 commit comments

Comments
 (0)