Skip to content

[code-completion] Don't try to equate/convert GenericTypeParameterTypes when computing type relations of completion results #15411

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

Closed
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ Swift 5.0
Swift 4.2
---------

* [SE-0054][]

`ImplicitlyUnwrappedOptional<T>` is now an unavailable typealias of `Optional<T>`.
Declarations annotated with `!` have the type `Optional<T>`. If an
expression involving one of these values will not compile successfully with the
type `Optional<T>`, it is implicitly unwrapped, producing a value of type `T`.

In some cases this change will cause code that previously compiled to
need to be adjusted. Please see [this blog post](https://swift.org/blog/iuo/)
for more information.

* [SE-0206][]

The standard library now uses a high-quality, randomly seeded, universal
Expand Down
8 changes: 4 additions & 4 deletions docs/WindowsBuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ set llvm_bin_dir="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windo
```

### 7. Build Swift
- This must be done from within a developer command prompt and could take up to
two hours depending on your system.
- This must be done from within a developer command prompt and could take hours
depending on your system.
- You may need to adjust the `SWIFT_WINDOWS_LIB_DIRECTORY` parameter depending on
your target platform or Windows SDK version.
```cmd
Expand Down Expand Up @@ -173,8 +173,8 @@ cmake -G "Visual Studio 15" "%swift_source_dir%/swift"^
```

### 8. Build lldb
- This must be done from within a developer command prompt and could take up to
two hours depending on your system.
- This must be done from within a developer command prompt and could take hours
depending on your system.
```cmd
mkdir "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/lldb-windows-amd64"
pushd "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/lldb-windows-amd64"
Expand Down
24 changes: 16 additions & 8 deletions include/swift/Reflection/TypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ enum class TypeRefKind {

// MSVC reports an error if we use "template"
// Clang reports an error if we don't use "template"
#if defined(__clang__) || defined(__GNUC__)
#define DEPENDENT_TEMPLATE template
#if __clang_major__ >= 7
#define DEPENDENT_TEMPLATE2
#if defined(__APPLE_CC__)
# define DEPENDENT_TEMPLATE template
# if __APPLE_CC__ >= 7000
# define DEPENDENT_TEMPLATE2
# else
# define DEPENDENT_TEMPLATE2 template
# endif
#elif defined(__clang__) || defined(__GNUC__)
# define DEPENDENT_TEMPLATE template
# if __clang_major__ >= 7
# define DEPENDENT_TEMPLATE2
# else
# define DEPENDENT_TEMPLATE2 template
# endif
#else
#define DEPENDENT_TEMPLATE2 template
#endif
#else
#define DEPENDENT_TEMPLATE
# define DEPENDENT_TEMPLATE template
# define DEPENDENT_TEMPLATE2
#endif

#define FIND_OR_CREATE_TYPEREF(Allocator, TypeRefTy, ...) \
Expand Down
13 changes: 9 additions & 4 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,15 @@ static CodeCompletionResult::ExpectedTypeRelation calculateTypeRelation(
Ty->is<ErrorType>() ||
ExpectedTy->is<ErrorType>())
return CodeCompletionResult::ExpectedTypeRelation::Unrelated;
if (Ty->isEqual(ExpectedTy))
return CodeCompletionResult::ExpectedTypeRelation::Identical;
if (isConvertibleTo(Ty, ExpectedTy, *DC))
return CodeCompletionResult::ExpectedTypeRelation::Convertible;

// Equality/Conversion of GenericTypeParameterType won't account for
// requirements – ignore them
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter()) {
if (Ty->isEqual(ExpectedTy))
return CodeCompletionResult::ExpectedTypeRelation::Identical;
if (isConvertibleTo(Ty, ExpectedTy, *DC))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at isConvertibleTo it has some flags to turn type parameters or archetypes into type variables - this will produce a more accurate result. Might just be a matter of reusing the existing mechanism

Copy link
Contributor Author

@nathawes nathawes Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried changing isConvertibleTo to canPossiblyConvertTo (which passes openArchetypes=true) but does that make sense here since the two type parameters can have different generic contexts?

E.g. with the below snippet:

func foo<U, V>(_ _: U, _ _: V) where U: Sequence, V: Sequence {}
func bar<U, T>(lhs: T, rhs: U) where T: Collection {
  _ = foo(lhs, #^COMPLETE^#
}

when adding type relations to the completion items for lhs we end up comparing Ty = T to ExpectedTy = V and isEqual and canPossiblyConvertTo both return true. For rhs we compare Ty = U to ExpectedTy = V where isEqual is false and canPossiblyConvertTo is true.

return CodeCompletionResult::ExpectedTypeRelation::Convertible;
}
if (auto FT = Ty->getAs<AnyFunctionType>()) {
if (FT->getResult()->isVoid())
return CodeCompletionResult::ExpectedTypeRelation::Invalid;
Expand Down
15 changes: 15 additions & 0 deletions stdlib/public/core/SequenceAlgorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,21 @@ extension Sequence {
}
return false
}

/// Returns a Boolean value indicating whether every element of a sequence
/// satisfies a given predicate.
///
/// - Parameter predicate: A closure that takes an element of the sequence
/// as its argument and returns a Boolean value that indicates whether
/// the passed element satisfies a condition.
/// - Returns: `true` if the sequence contains only elements that satisfy
/// `predicate`; otherwise, `false`.
@inlinable
public func allSatisfy(
_ predicate: (Element) throws -> Bool
) rethrows -> Bool {
return try !contains { try !predicate($0) }
}
}

extension Sequence where Element : Equatable {
Expand Down
10 changes: 10 additions & 0 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=BOUND_IUO | %FileCheck %s -check-prefix=MEMBEROF_IUO
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=FORCED_IUO | %FileCheck %s -check-prefix=MEMBEROF_IUO

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_TO_GENERIC | %FileCheck %s -check-prefix=GENERIC_TO_GENERIC

var i1 = 1
var i2 = 2
var oi1 : Int?
Expand Down Expand Up @@ -373,6 +375,14 @@ struct TestBoundGeneric1 {
// BOUND_GENERIC_1: Decl[InstanceVar]/CurrNominal: y[#[Int]#];
}

func whereConvertible<T>(lhs: T, rhs: T) where T: Collection {
_ = zip(lhs, #^GENERIC_TO_GENERIC^#)
}
// GENERIC_TO_GENERIC: Begin completions
// GENERIC_TO_GENERIC: Decl[LocalVar]/Local: lhs[#Collection#]; name=lhs
// GENERIC_TO_GENERIC: Decl[LocalVar]/Local: rhs[#Collection#]; name=rhs
// GENERIC_TO_GENERIC: End completions

func emptyOverload() {}
func emptyOverload(foo foo: Int) {}
emptyOverload(foo: #^EMPTY_OVERLOAD_1^#)
Expand Down
14 changes: 14 additions & 0 deletions validation-test/stdlib/SequenceType.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ SequenceTypeTests.test("contains/Predicate") {
}
}

SequenceTypeTests.test("allSatisfy/Predicate") {
for test in findTests {
let s = MinimalSequence<OpaqueValue<Int>>(
elements: test.sequence.map { OpaqueValue($0.value) })
expectEqual(
test.expected == nil,
s.allSatisfy { $0.value != test.element.value },
stackTrace: SourceLocStack().with(test.loc))
expectEqual(
test.expectedLeftoverSequence.map { $0.value }, s.map { $0.value },
stackTrace: SourceLocStack().with(test.loc))
}
}

//===----------------------------------------------------------------------===//
// reduce()
//===----------------------------------------------------------------------===//
Expand Down