Skip to content

[5.3] Revise #file changes from SE-0274 #32811

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

Merged
merged 10 commits into from
Jul 17, 2020

Conversation

beccadax
Copy link
Contributor

Cherry-picks #32700 to release/5.3.

This PR implements the changes discussed in the follow-up thread to SE-0274 (now SE-0285). Specifically, it:

  • Fixes several tests which previously violated the same-filename rule, but got away with it because they were directly calling the frontend. Also tightens up a test previously introduced by [NFC] GenericSignatureImpl: Spell conformsToProtocol & getConformsTo in terms of requirements #31734 so that it merely ensures filenames don't end up in SIL debug info, rather than in the SIL file at all.

  • Refactors a lot of magic identifier literal implementation to be driven by a new MagicIdentifierKinds.def file. MagicIdentifierLiteralExpr::Kind and some cases of DefaultArgumentKind are now driven by this table, as is much logic that simply propagates these kinds without changing them; it also specifies tokens related to magic identifier literals, so parsing and code completion now work from this file too. (It also fixes a bug in #filePath's code completion support, which was previously disabled and untested.)

  • Adds a new #fileID literal which always generates the concise file ID string.

  • Splits MagicIdentifierLiteralExpr::Kind::File (and similar cases elsewhere) into FilePathSpelledAsFile and FileIDSpelledAsFile. When -enable-experimental-concise-pound-file is false, the parser generates the former, producing current Swift 5 behavior; when it is true, the parser generates the latter, producing future "Swift 6" behavior. The rest of the compiler (including module interface emission, by preserving the compiler flag) propagates this difference so that Swift 5 declarations can be used from Swift 6 code and vice-versa without confusion. This design causes more code changes, but is less risky than alternatives.

  • Introduces a concept of "compatibility" to the diag::default_magic_identifier_mismatch diagnostic. If two magic identifiers are compatible, the diagnostic is suppressed. Compatibility is commutative. Every magic identifier is compatible with itself; Swift 6 #file is compatible with #fileID, while Swift 5 #file is compatible with #fileID, #filePath, and Swift 6 #file.

  • Makes both compiler-generated traps and standard library diagnostics use file IDs instead of file paths. In the standard library, this is temporarily being done by building with -enable-experimental-concise-pound-file, which is supported by compilers since Shorten #file and add #filePath (behind an experimental flag) #25656 in late December, rather than by actually writing #fileID, which is introduced in this change. This is merely to give updated tools time to spread; I intend to remove the flag and write #fileID in the future.

Fixes rdar://65114002 and SR-12936.

beccadax added 10 commits July 9, 2020 23:08
The meaning of EnableConcisePoundFile is going to shift slightly, so it makes sense to always include #filePath in completions. Also, @rintaro confirmed that this should be using KeywordKind::pound_filePath, not KeywordKind::pound_file.
Extracts the list of magic identifier literal kinds into a separate file and updates a lot of code to use macro metaprogramming instead of naming half a dozen cases manually. This is a complicated change, but it should be NFC.
Doing this NFC renaming first helps clarify the functional changes to come.

# Conflicts:
#	lib/SILGen/SILGenConvert.cpp
…and modify resolveFileIDConflicts() to diagnose any such violations instead of asserting.

Swift does not allow any two files in the same module to have the same filename, even if they are in different directories. However, this is enforced in the driver, so tests that invoke the frontend directly can violate it. Turns out that a couple of those snuck into the test suite at various points.

This commit updates those tests. It also causes the frontend to diagnose the duplicate filename error just as the driver would have, which should help us understand what happened more easily if this crops up again in the future.

NFC, since invoking the frontend directly is unsupported.
This temporarily breaks -enable-experimental-concise-pound-file.

fixup adding #fileID

# Conflicts:
#	lib/SILGen/SILGenConvert.cpp
Such as force unwraps, as! casts, etc.

# Conflicts:
#	lib/SILGen/SILGenConvert.cpp
In -swift-version 5 and earlier, #file will continue to be a synonym for #filePath; in a future -swift-version (“Swift 6 mode”), it will become a synonym for #fileID. #file in libraries will be interpreted according to the language mode the library was compiled in, not the language mode its client uses.

Implement this behavior, tied to a frontend flag instead of a language version. We do so by splitting the old `MagicIdentifierLiteralExprKind::File` into two separate cases, `FileIDSpelledAsFile` and `FilePathSpelledAsFile`, and propagating this distinction throughout the AST. This seems cleaner than looking up the setting for the module the declaration belongs to every time we see `File`.

This doesn’t handle module interfaces yet; we’ll take care of those in a separate commit.

# Conflicts:
#	lib/Serialization/ModuleFormat.h
Add -experimental-enable-concise-pound-file to the list of flags preserved by module interfaces, so that when we rebuild an interface, it comes out the same way as the original file.
This change makes:

* #file compatible with #fileID in “Swift 6 mode”
* #file compatible with #filePath and #fileID in Swift 5 mode
* #file in Swift 5 mode code compatible with #file in “Swift 6 mode” code

This should keep anyone from seeing XCTAssert-wrapping noise until they adopt “Swift 6 mode” (whatever version that ends up actually being).
We ultimately want to explicitly change standard library uses of #file to #fileID, but once we do, previous compilers won’t be able to build the standard library. So instead, we will temporarily build the standard library with -enable-experimental-concise-pound-file, which should have the same effect, but will back-deploy to compilers going back several months.
@beccadax beccadax added swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review r5.3 labels Jul 10, 2020
@beccadax beccadax requested a review from a team as a code owner July 10, 2020 06:44
@beccadax beccadax marked this pull request as draft July 10, 2020 06:44
@beccadax
Copy link
Contributor Author

With swiftlang/swift-syntax#227

@swift-ci please test

@beccadax beccadax added swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process and removed swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review labels Jul 17, 2020
@beccadax beccadax marked this pull request as ready for review July 17, 2020 16:53
@beccadax
Copy link
Contributor Author

With swiftlang/swift-syntax#227

@swift-ci please test

@beccadax beccadax requested review from devincoughlin and tomerd July 17, 2020 18:35
@beccadax beccadax merged commit e948699 into swiftlang:release/5.3 Jul 17, 2020
benrimmington added a commit to swiftlang/swift-evolution that referenced this pull request Jul 18, 2020
beccadax added a commit to beccadax/swift that referenced this pull request Aug 11, 2020
This tweaks the implementation of SE-0285 to preserve backwards compatibility with the previous module serialization format. Existing default arguments all keep their previous values when serialized; #fileID and Swift 6 #file are given previously unused values.

Fixes rdar://66810027.
tkremenek added a commit that referenced this pull request Aug 14, 2020
@AnthonyLatsis AnthonyLatsis added swift 5.3 🍒 release cherry pick Flag: Release branch cherry picks labels Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process swift 5.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants