Skip to content

[SR-4207] Type system allows same-type constraint to protocol w/ Self requirement #46790

Closed
@natecook1000

Description

@natecook1000
Previous ID SR-4207
Radar rdar://23427259
Original Reporter @natecook1000
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 8.3b2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 74d9820b2f361986d6c62b6fcddc6c05

is duplicated by:

  • SR-9221 Superfluous senseless diagnostic on unsupported constrained extension of generic type

Issue Description:

You can currently write a generic function that uses a same-type constraint with an associated type and a protocol with a Self requirement, which typically can only be used as a constraint, not as a type. This can lead to some strange behavior:

func foo<S: Sequence where S.Iterator.Element == Equatable>(_ s: S) {
    var iterator = s.makeIterator()
    let x = iterator.next()!
    // let y: Equatable = iterator.next()!
}

Inside foo(_:), x is statically typed as Equatable, which you normally can't make happen. If you uncomment the line that declares y, which doesn't rely on type inference, you rightly get an error. foo(_:) isn't actually callable, since the same limitations prevent you from actually creating a sequence whose elements have the type Equatable, but the compiler should give an error when writing this function--the author almost certainly means to use the protocol in a "conforms to" relationship.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions