-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
| Previous ID | SR-11373 |
| Radar | None |
| Original Reporter | @hamishknight |
| Type | Bug |
Environment
Swift version 5.1-dev (LLVM c5340df2d1, Swift 98a54a1)
Target: x86_64-apple-darwin18.5.0
Additional Detail from JIRA
| Votes | 1 |
| Component/s | Compiler |
| Labels | Bug, TypeChecker |
| Assignee | None |
| Priority | Medium |
md5: 13080b3b09be239733bd1a724a240c24
Issue Description:
Came up with the following bit of cursed Swift this morning:
print(min("b", "a")) // a
print(String(validatingUTF8: min("b", "a"))!) // bThis happens because min's generic parameter T gets inferred to be an UnsafePointer, resulting in string-to-pointer conversions for its arguments (and the first arg just happens to have a lower address). It then leads to undefined behaviour from min returning a now dangling pointer.
Arguably we should ban X-to-pointer conversions for generic parameters that happen to be inferred as pointer types, such as the following:
func id<T>(_ x: T) -> T { x }
let ptr: UnsafePointer<CChar> = id("hello")I don't know how source breaking such a change would be though. The two examples given above are undefined behaviour, but it could be possible that somebody is using this mechanic in a well-defined manner (I can't think of any particularly motivating examples off the top of my head though). Would such a change require an SE proposal?