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

Warn on implicit raw pointer conversion to non-bitwise-copyable values #63825

Merged
merged 4 commits into from
Mar 2, 2023

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Feb 22, 2023

Fixes a usability problem with implicit inout conversion to raw pointers.

For example, supporting this conversion turns out to be bad:

    void read_void(const void *input);

    func foo(data: inout Data) {
        read_void(&data)
    }

People understandably expect Foundation.Data to have the same sort of
implicit conversion as Array. But it does something very wrong
instead.

We could have added an an attribute to Data and other copy-on-write
containers to selectively suppress implicit conversion. But there is
no good reason to allow implicit conversion from any non-trivial
type. It is extremely dangerous, and almost always accidental. Note
that this problem becomes worse now that the compiler views imported
`char *` arguments as raw pointers. For example:

  void read_char(const char *input);

  var object: AnyObject = ...
  read_void(&object1)

This seems like a good time to correct this old Swift 3 behavior.

Plan: Add a warning now. Convert it to an error in Swift 6 language
mode based on feedback.

Fixes rdar://97963116 (It's really easy to accidentally corrupt a Data
object with the & operator)

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci test

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci toolchain

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci test source compatibility

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci Please Build Toolchain macOS Platform

@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from d65a6a4 to 6cf0e2e Compare February 22, 2023 17:52
@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci test

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci Please Build Toolchain macOS Platform

@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci test source compatibility

@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from 6cf0e2e to 54f7aad Compare February 22, 2023 21:30
@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci test

@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from 54f7aad to 9b30bff Compare February 22, 2023 22:08
@atrick
Copy link
Contributor Author

atrick commented Feb 22, 2023

@swift-ci smoke test

@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from 9b30bff to 1ecc3f8 Compare February 23, 2023 00:07
@atrick
Copy link
Contributor Author

atrick commented Feb 23, 2023

@swift-ci smoke test

@atrick
Copy link
Contributor Author

atrick commented Feb 23, 2023

@swift-ci smoke test macOS platform

@atrick
Copy link
Contributor Author

atrick commented Feb 23, 2023

@swift-ci build toolchain

@atrick atrick marked this pull request as ready for review February 23, 2023 16:56
Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

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

This looks good. Thanks!

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

Sema related changes look good to me!

lib/Sema/CSGen.cpp Outdated Show resolved Hide resolved
@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from 1ecc3f8 to 2c60527 Compare February 28, 2023 05:24
@atrick
Copy link
Contributor Author

atrick commented Feb 28, 2023

@swift-ci test

This is also needed in SIL diagnostics, not just Sema diagnostics,
because implicit Array conversion generates special SIL patterns.
Fixes a usability problem with implicit inout conversion to raw pointers.

For example, supporting this conversion turns out to be bad:

    void read_void(const void *input);

    func foo(data: inout Data) {
        read_void(&data)
    }

People understandably expect Foundation.Data to have the same sort of
implicit conversion as Array. But it does something very wrong
instead.

We could have added an an attribute to Data and other copy-on-write
containers to selectively suppress implicit conversion. But there is
no good reason to allow implicit conversion from any non-trivial
type. It is extremely dangerous, and almost always accidental. Note
that this problem becomes worse now that the compiler views imported
`char *` arguments as raw pointers. For example:

  void read_char(const char *input);

  var object: AnyObject = ...
  read_void(&object1)

This seems like a good time to correct this old Swift 3 behavior.

Plan: Add a warning now. Convert it to an error in Swift 6 language
mode based on feedback.

Fixes rdar://97963116 (It's really easy to accidentally corrupt a Data
object with the & operator)
@atrick atrick force-pushed the diagnose-implicit-raw-bitwise branch from 2c60527 to a354f26 Compare February 28, 2023 06:00
@atrick
Copy link
Contributor Author

atrick commented Feb 28, 2023

@swift-ci test

@atrick
Copy link
Contributor Author

atrick commented Feb 28, 2023

@swift-ci toolchain

@atrick
Copy link
Contributor Author

atrick commented Feb 28, 2023

@swift-ci test source compatibility

@atrick
Copy link
Contributor Author

atrick commented Mar 2, 2023

@swift-ci smoke test

@atrick atrick merged commit f1ff695 into swiftlang:main Mar 2, 2023
@atrick atrick deleted the diagnose-implicit-raw-bitwise branch March 2, 2023 18:57
@atrick atrick changed the title Warn on implicit pointer conversion from nontrivial inout values. Constrain implicit raw pointer conversion to bitwise-copyable values Apr 5, 2023
@atrick atrick changed the title Constrain implicit raw pointer conversion to bitwise-copyable values Warn on implicit raw pointer conversion to non-bitwise-copyable values Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants