Skip to content

IRGen: Type reconstruction supports opaque result types now #30861

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 5 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ class ASTMangler : public Mangler {

std::string mangleLocalTypeDecl(const TypeDecl *type);

std::string mangleOpaqueTypeDecl(const OpaqueTypeDecl *decl);

enum SpecialContext {
ObjCContext,
ClangImporterContext,
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/Basic/Defer.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/ManglingUtils.h"
#include "swift/Demangling/Demangler.h"
#include "swift/Strings.h"
Expand Down Expand Up @@ -691,6 +692,12 @@ std::string ASTMangler::mangleLocalTypeDecl(const TypeDecl *type) {
return finalize();
}

std::string ASTMangler::mangleOpaqueTypeDecl(const OpaqueTypeDecl *decl) {
DWARFMangling = true;
OptimizeProtocolNames = false;
return mangleDeclAsUSR(decl->getNamingDecl(), MANGLING_PREFIX_STR);
}

void ASTMangler::appendSymbolKind(SymbolKind SKind) {
switch (SKind) {
case SymbolKind::Default: return;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7088,7 +7088,7 @@ Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
{
llvm::raw_svector_ostream os(mangleBuf);
Mangle::ASTMangler mangler;
os << mangler.mangleDeclAsUSR(getNamingDecl(), MANGLING_PREFIX_STR);
os << mangler.mangleOpaqueTypeDecl(this);
}

OpaqueReturnTypeIdentifier = getASTContext().getIdentifier(mangleBuf);
Expand Down
4 changes: 1 addition & 3 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,9 +798,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
std::string Result = Mangler.mangleTypeForDebugger(
Ty, nullptr);

if (!Opts.DisableRoundTripDebugTypes
// FIXME: implement type reconstruction for opaque types
&& !Ty->hasOpaqueArchetype()) {
if (!Opts.DisableRoundTripDebugTypes) {
// Make sure we can reconstruct mangled types for the debugger.
#ifndef NDEBUG
auto &Ctx = Ty->getASTContext();
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void swift::simple_display(llvm::raw_ostream &out,
} else {
assert(SF);
out << "IR Generation for file ";
out << '\"' << cast<LoadedFile>(SF)->getFilename() << '\"';
out << '\"' << SF->getFilename() << '\"';
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,14 @@ EmittedMembersRequest::evaluate(Evaluator &evaluator,
forceConformance(Context.getProtocol(KnownProtocolKind::Encodable));
forceConformance(Context.getProtocol(KnownProtocolKind::Hashable));

// The projected storage wrapper ($foo) might have dynamically-dispatched
// accessors, so force them to be synthesized.
for (auto *member : CD->getMembers()) {
if (auto *var = dyn_cast<VarDecl>(member))
if (var->hasAttachedPropertyWrapper())
(void) var->getPropertyWrapperBackingProperty();
}

return CD->getMembers();
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ class ModuleFile::LocalDeclTableInfo {
return ID;
}

external_key_type GetExternalKey(internal_key_type ID) {
return ID;
}

hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, SWIFTMODULE_HASH_SEED);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 552; // simple didSet
const uint16_t SWIFTMODULE_VERSION_MINOR = 553; // change to USR mangling

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down
3 changes: 1 addition & 2 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5067,8 +5067,7 @@ void Serializer::writeAST(ModuleOrSourceFile DC) {
for (auto OTD : opaqueReturnTypeDecls) {
hasOpaqueReturnTypes = true;
Mangle::ASTMangler Mangler;
auto MangledName = Mangler.mangleDeclAsUSR(OTD->getNamingDecl(),
MANGLING_PREFIX_STR);
auto MangledName = Mangler.mangleOpaqueTypeDecl(OTD);
opaqueReturnTypeGenerator.insert(MangledName, addDeclRef(OTD));
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/DebugInfo/opaque_result_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -emit-ir -g %s -disable-availability-checking

public protocol P {
associatedtype Horse
}

public protocol Feed {}

public struct Hay : Feed {}

public func hasOpaqueResult<T : P>(_: T.Type, _: T.Horse) -> some Feed {
return Hay()
}


22 changes: 22 additions & 0 deletions test/SILGen/Inputs/property_wrappers_multifile_other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class MyClass {
public init() { }

@PropertyWrapper()
public var instanceProperty: Bool
}

@propertyWrapper
public struct PropertyWrapper {
public var projectedValue: PropertyWrapper {
get {
return self
}
set {
self = newValue
}
}

public var wrappedValue: Bool {
return false
}
}
12 changes: 12 additions & 0 deletions test/SILGen/property_wrappers_multifile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-emit-silgen -primary-file %s %S/Inputs/property_wrappers_multifile_other.swift | %FileCheck %s

public class YourClass : MyClass {}

// CHECK-LABEL: sil_vtable [serialized] YourClass {
// CHECK-NEXT: #MyClass.init!allocator: (MyClass.Type) -> () -> MyClass : @$s27property_wrappers_multifile9YourClassCACycfC [override]
// CHECK-NEXT: #MyClass.instanceProperty!getter: (MyClass) -> () -> Bool : @$s27property_wrappers_multifile7MyClassC16instancePropertySbvg [inherited]
// CHECK-NEXT: #MyClass.$instanceProperty!getter: (MyClass) -> () -> PropertyWrapper : @$s27property_wrappers_multifile7MyClassC17$instancePropertyAA0G7WrapperVvg [inherited]
// CHECK-NEXT: #MyClass.$instanceProperty!setter: (MyClass) -> (PropertyWrapper) -> () : @$s27property_wrappers_multifile7MyClassC17$instancePropertyAA0G7WrapperVvs [inherited]
// CHECK-NEXT: #MyClass.$instanceProperty!modify: (MyClass) -> () -> () : @$s27property_wrappers_multifile7MyClassC17$instancePropertyAA0G7WrapperVvM [inherited]
// CHECK-NEXT: #YourClass.deinit!deallocator: @$s27property_wrappers_multifile9YourClassCfD
// CHECK-NEXT: }
33 changes: 33 additions & 0 deletions test/multifile/Inputs/rdar61229365.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class MyClass {
public init() { }

@PropertyWrapper(defaultValue: false)
public var wrappedProperty: Bool

public func check() {
let foo = $wrappedProperty
$wrappedProperty = foo
}
}

@propertyWrapper
public struct PropertyWrapper<Value> {
public let defaultValue: Value

public var projectedValue: PropertyWrapper<Value> {
get {
return self
}
set {
self = newValue
}
}

public var wrappedValue: Value {
return defaultValue
}

public init(defaultValue: Value) {
self.defaultValue = defaultValue
}
}
1 change: 0 additions & 1 deletion test/multifile/Inputs/sr12429.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public struct PropertyWrapper<Value> {
get {
return self
}
// Having this setter is what causes the mis-compilation
set {
self = newValue
}
Expand Down
16 changes: 16 additions & 0 deletions test/multifile/property-wrappers-rdar61229365.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -o %t/main %t/main.swift %S/Inputs/rdar61229365.swift
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main

// REQUIRES: executable_test

class YourClass : MyClass {
override init() {
super.init()
}
}

let object = YourClass()
object.check()