Skip to content

Commit 7d082d4

Browse files
authored
Merge pull request #1996 from swiftwasm/main
[pull] swiftwasm from main
2 parents d050ae7 + 6a19d37 commit 7d082d4

File tree

9 files changed

+218
-3
lines changed

9 files changed

+218
-3
lines changed

include/swift/Sema/ConstraintSystem.h

+1
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,7 @@ class ConstraintSystem {
49424942
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
49434943
Optional<PotentialBindings> determineBestBindings();
49444944

4945+
public:
49454946
/// Infer bindings for the given type variable based on current
49464947
/// state of the constraint system.
49474948
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,

lib/Sema/TypeCheckAvailability.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2900,9 +2900,10 @@ static bool declNeedsExplicitAvailability(const Decl *decl) {
29002900
return false;
29012901
}
29022902

2903-
// Skip functions emitted into clients or SPI.
2903+
// Skip functions emitted into clients, SPI or implicit.
29042904
if (decl->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>() ||
2905-
decl->isSPI())
2905+
decl->isSPI() ||
2906+
decl->isImplicit())
29062907
return false;
29072908

29082909
// Warn on decls without an introduction version.

stdlib/public/Concurrency/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
2929
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
3030
-parse-stdlib
3131
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
32-
INSTALL_IN_COMPONENT stdlib)
32+
INSTALL_IN_COMPONENT stdlib
33+
)

test/attr/require_explicit_availability.swift

+8
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,11 @@ extension SomeClass { // expected-warning {{public declarations should have an a
164164
set(newValue) { }
165165
}
166166
}
167+
168+
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
169+
public struct StructWithImplicitMembers { }
170+
171+
extension StructWithImplicitMembers: Hashable { }
172+
// expected-note @-1 {{add @available attribute to enclosing extension}}
173+
// expected-warning @-2 {{public declarations should have an availability attribute when building with -require-explicit-availability}}
174+
// expected-error @-3 {{'StructWithImplicitMembers' is only available in macOS 10.15 or newer}}

unittests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if(SWIFT_INCLUDE_TOOLS)
1212
add_subdirectory(Localization)
1313
add_subdirectory(IDE)
1414
add_subdirectory(Parse)
15+
add_subdirectory(Sema)
1516
add_subdirectory(SwiftDemangle)
1617
add_subdirectory(Syntax)
1718
if(SWIFT_BUILD_SYNTAXPARSERLIB)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- BindingInferenceTests.cpp ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SemaFixture.h"
14+
#include "swift/AST/Expr.h"
15+
#include "swift/Sema/ConstraintSystem.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
using namespace swift::constraints;
20+
21+
TEST_F(SemaTest, TestIntLiteralBindingInference) {
22+
ConstraintSystemOptions options;
23+
options |= ConstraintSystemFlags::AllowUnresolvedTypeVariables;
24+
25+
ConstraintSystem cs(DC, options);
26+
27+
auto *intLiteral = IntegerLiteralExpr::createFromUnsigned(Context, 42);
28+
29+
auto *literalTy = cs.createTypeVariable(cs.getConstraintLocator(intLiteral),
30+
/*options=*/0);
31+
32+
cs.addConstraint(
33+
ConstraintKind::LiteralConformsTo, literalTy,
34+
Context.getProtocol(KnownProtocolKind::ExpressibleByIntegerLiteral)
35+
->getDeclaredInterfaceType(),
36+
cs.getConstraintLocator(intLiteral));
37+
38+
auto bindings = cs.inferBindingsFor(literalTy);
39+
40+
ASSERT_EQ(bindings.Bindings.size(), (unsigned)1);
41+
42+
const auto &binding = bindings.Bindings.front();
43+
44+
ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int")));
45+
ASSERT_TRUE(binding.hasDefaultedLiteralProtocol());
46+
}

unittests/Sema/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
add_swift_unittest(swiftSemaTests
3+
SemaFixture.cpp
4+
BindingInferenceTests.cpp)
5+
6+
target_link_libraries(swiftSemaTests
7+
PRIVATE
8+
swiftAST
9+
swiftSema
10+
swiftSerialization)
11+
12+
target_compile_definitions(swiftSemaTests PRIVATE
13+
SWIFTLIB_DIR=\"${SWIFTLIB_DIR}\")

unittests/Sema/SemaFixture.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===--- SemaFixture.cpp - Helper for setting up Sema context --------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SemaFixture.h"
14+
#include "swift/AST/Decl.h"
15+
#include "swift/AST/Import.h"
16+
#include "swift/AST/Module.h"
17+
#include "swift/AST/ParseRequests.h"
18+
#include "swift/AST/SourceFile.h"
19+
#include "swift/AST/Type.h"
20+
#include "swift/AST/Types.h"
21+
#include "swift/Basic/LLVMInitialize.h"
22+
#include "swift/ClangImporter/ClangImporter.h"
23+
#include "swift/Serialization/SerializedModuleLoader.h"
24+
#include "swift/Subsystems.h"
25+
26+
using namespace swift;
27+
using namespace swift::unittest;
28+
29+
SemaTest::SemaTest()
30+
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts,
31+
ClangImporterOpts, SourceMgr, Diags)) {
32+
INITIALIZE_LLVM();
33+
34+
registerParseRequestFunctions(Context.evaluator);
35+
registerTypeCheckerRequestFunctions(Context.evaluator);
36+
37+
Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context));
38+
Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true);
39+
40+
auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true);
41+
assert(stdlib && "Failed to load standard library");
42+
43+
auto *module =
44+
ModuleDecl::create(Context.getIdentifier("SemaTests"), Context);
45+
46+
MainFile = new (Context) SourceFile(*module, SourceFileKind::Main,
47+
/*buffer=*/None);
48+
49+
AttributedImport<ImportedModule> stdlibImport{{ImportPath::Access(), stdlib},
50+
/*options=*/{}};
51+
52+
MainFile->setImports(stdlibImport);
53+
module->addFile(*MainFile);
54+
55+
DC = module;
56+
}
57+
58+
Type SemaTest::getStdlibType(StringRef name) const {
59+
auto typeName = Context.getIdentifier(name);
60+
61+
auto *stdlib = Context.getStdlibModule();
62+
63+
llvm::SmallVector<ValueDecl *, 4> results;
64+
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results);
65+
66+
if (results.size() != 1)
67+
return Type();
68+
69+
if (auto *decl = dyn_cast<TypeDecl>(results.front())) {
70+
if (auto *NTD = dyn_cast<NominalTypeDecl>(decl))
71+
return NTD->getDeclaredType();
72+
return decl->getDeclaredInterfaceType();
73+
}
74+
75+
return Type();
76+
}

unittests/Sema/SemaFixture.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- SemaFixture.h - Helper for setting up Sema context -----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/ASTContext.h"
14+
#include "swift/AST/DiagnosticEngine.h"
15+
#include "swift/AST/Module.h"
16+
#include "swift/AST/SourceFile.h"
17+
#include "swift/AST/Type.h"
18+
#include "swift/Basic/LangOptions.h"
19+
#include "swift/Basic/Platform.h"
20+
#include "swift/Basic/SourceManager.h"
21+
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/ADT/SmallString.h"
23+
#include "llvm/Support/Host.h"
24+
#include "llvm/Support/Path.h"
25+
#include "gtest/gtest.h"
26+
#include <string>
27+
28+
namespace swift {
29+
namespace unittest {
30+
31+
class SemaTestBase : public ::testing::Test {
32+
public:
33+
LangOptions LangOpts;
34+
TypeCheckerOptions TypeCheckerOpts;
35+
SearchPathOptions SearchPathOpts;
36+
ClangImporterOptions ClangImporterOpts;
37+
SourceManager SourceMgr;
38+
DiagnosticEngine Diags;
39+
40+
SemaTestBase() : Diags(SourceMgr) {
41+
LangOpts.Target = llvm::Triple(llvm::sys::getDefaultTargetTriple());
42+
43+
llvm::SmallString<128> libDir(SWIFTLIB_DIR);
44+
llvm::sys::path::append(libDir, getPlatformNameForTriple(LangOpts.Target));
45+
46+
SearchPathOpts.RuntimeResourcePath = SWIFTLIB_DIR;
47+
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(libDir.str()));
48+
SearchPathOpts.RuntimeLibraryImportPaths.push_back(
49+
std::string(libDir.str()));
50+
}
51+
};
52+
53+
/// Owns an ASTContext and the associated types.
54+
class SemaTest : public SemaTestBase {
55+
SourceFile *MainFile;
56+
57+
public:
58+
ASTContext &Context;
59+
DeclContext *DC;
60+
61+
SemaTest();
62+
63+
protected:
64+
Type getStdlibType(StringRef name) const;
65+
};
66+
67+
} // end namespace unittest
68+
} // end namespace swift

0 commit comments

Comments
 (0)