Skip to content
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

[SR-11373] Don't perform X-to-pointer conversions for generic params inferred to be pointer types #53774

Open
hamishknight opened this issue Aug 25, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

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"))!) // b

This 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?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant