Skip to content

[Migrator] Add Swift. prefix to type(of:) expressions in Swift 3 #9362

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 1 commit into from
May 6, 2017
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
38 changes: 38 additions & 0 deletions lib/Migrator/SyntacticMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "swift/AST/NameLookup.h"
#include "swift/AST/USRGeneration.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/Frontend/Frontend.h"
Expand Down Expand Up @@ -536,6 +537,38 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
return;
}

/// If the expression is `type(of:)`, in Swift 3 it's a special 'dynamic type
/// expression'. In Swift 4, this is resolved by overload resolution, so
/// if there is a func type(of:) or even var type, it can shadow
/// Swift.type(of:), which is probably not what people want.
///
/// This migration operation adds `Swift.` to disambiguate in Swift 4.
void handleTypeOfFunction(const DynamicTypeExpr *DTE) {
if (!SF->getASTContext().LangOpts.isSwiftVersion3()) {
return;
}
const auto CSR = Lexer::getCharSourceRangeFromSourceRange(SM,
DTE->getSourceRange());
if (SM.extractText(CSR).startswith("Swift.")) {
return;
}

UnqualifiedLookup Lookup {
{ SF->getASTContext().getIdentifier("type") },
SF->getModuleScopeContext(),
/*TypeResolver=*/nullptr,
/*IsKnownPrivate=*/false,
DTE->getLoc()
};
if (Lookup.Results.empty()) {
// There won't be a name shadowing here in Swift 4, so we don't need to
// do anything.
return;
}

Editor.insertBefore(DTE->getLoc(), "Swift.");
}

bool walkToExprPre(Expr *E) override {
if (auto *CE = dyn_cast<CallExpr>(E)) {
handleTupleArgumentMismatches(CE);
Expand Down Expand Up @@ -566,6 +599,11 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
break;
}
}

if (const auto *DTE = dyn_cast<DynamicTypeExpr>(E)) {
handleTypeOfFunction(DTE);
}

return true;
}

Expand Down
27 changes: 27 additions & 0 deletions test/Migrator/prefix_typeof_expr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// rdar://problem/32025974
// XFAIL: linux
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/prefix_typeof_expr.swift.result -emit-remap-file-path %t/member.swift.remap -o /dev/null
// RUN: diff -u %S/prefix_typeof_expr.swift.expected %t/prefix_typeof_expr.swift.result

class HasTypeVar {
var type: Int {
precondition(true, "\(type(of: self)) should never be asked for its type")
_ = Swift.type(of: 1) // Don't add another prefix to this one
return 1
}
}

class HasTypeMethod {
func type<T>(of: T) -> Int {
_ = type(of: 1)
_ = Swift.type(of: 1) // Don't add another prefix to this one
return 1
}
}

func type<T>(of: T) -> Int {
return 1
}

_ = type(of: 1)
_ = Swift.type(of: 1) // Don't add another prefix to this one
27 changes: 27 additions & 0 deletions test/Migrator/prefix_typeof_expr.swift.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// rdar://problem/32025974
// XFAIL: linux
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/prefix_typeof_expr.swift.result -emit-remap-file-path %t/member.swift.remap -o /dev/null
// RUN: diff -u %S/prefix_typeof_expr.swift.expected %t/prefix_typeof_expr.swift.result

class HasTypeVar {
var type: Int {
precondition(true, "\(Swift.type(of: self)) should never be asked for its type")
_ = Swift.type(of: 1) // Don't add another prefix to this one
return 1
}
}

class HasTypeMethod {
func type<T>(of: T) -> Int {
_ = Swift.type(of: 1)
_ = Swift.type(of: 1) // Don't add another prefix to this one
return 1
}
}

func type<T>(of: T) -> Int {
return 1
}

_ = Swift.type(of: 1)
_ = Swift.type(of: 1) // Don't add another prefix to this one
2 changes: 1 addition & 1 deletion test/Migrator/tuple-arguments.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// FIXME: Figure out why this is not working on linux
// rdar://problem/32025974
// XFAIL: linux

// RUN: %target-swift-frontend -typecheck %s -swift-version 3
Expand Down
2 changes: 1 addition & 1 deletion test/Migrator/tuple-arguments.swift.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// FIXME: Figure out why this is not working on linux
// rdar://problem/32025974
// XFAIL: linux

// RUN: %target-swift-frontend -typecheck %s -swift-version 3
Expand Down