I like the unified @linkage
spelling, and thanks for continuing to push this forward. A couple of comments and requests.
If you're looking for another use case in addition to the excellent @DebugDescription
macro, swift-testing would use this to record metadata about each of the tests so that it can find them at runtime.
I feel like the feature "linker sets" isn't something that the document can rely on people already knowing, and your references to it aren't buying much. Perhaps this document could just focus on the linkage part and avoid "linker sets" entirely, noting only that extraction of information from specific sections is a separate issue?
I'm torn on some of the restrictions that are placed on the use of @linkage
such as:
- the variable must not be declared inside a generic context (either directly in generic type or nested in a generic type)
- the initial expression assigned to the variable must be statically evaluable (see below)
The statically-evaluable requirement seems orthogonal to the specification of linkage. You could certainly want to be able to require that a variable be statically evaluable in cases where you don't need linkage (i.e., all you care is that there's no runtime initialization for the variable), and it seems plausible that you might not care so much about statically evaluable for other uses of linkage.
At the moment, static variables aren't even allowed within generic contexts, so I'm not sure you need to call out this limitation on use in generics.
I think we should pull weak
into this proposal. We already have the underscored @_weakLinked
attribute, so we'd be standardizing that. Weak linkage also applies to more than functions and global variables (it works for almost anything), so it will require some generalization in this proposal.
I'm somewhat inclined to want to pull mangledName
(perhaps call it symbolName
) into this proposal, again because it standardizes something that's existed for a long time as an underscored attribute (@_silgen_name
), and is very much something one wants to do when in the realm of linkage specifications.
I find this to be unfortunate:
#if os(macOS)
@linkage(section: "__DATA,mysection")
#elif os(Linux)
@linkage(section: ".mysection")
#endif
var global: Int = 42
because (1) it's really easy to make a mistake and write a non-portable section name that's going to blow up on another platform (e.g., Windows requires very short names), and (2) the link between os
strings and object file format is arcane knowledge. There are a couple of paths we could take here. We could add #if
support for specific object file formats, e.g.,
#if objectFile(MachO)
@linkage(section: "__DATA,mysection")
#elif objectFile(ELF)
@linkage(section: ".mysection")
#endif
var global: Int = 42
which potentially would help other code that needs to look at the loaded image, such as the APIs to enumerate the entries in a given section that are mentioned in Future Directions. Or we could try to encode the information in the @linkage
attribute itself, e.g.,
@linkage(section: [.macho: "__DATA,mysection", .elf: ".mysection"])
which eliminates duplication of the attributes and allows validation of all of the names regardless of what platform you compile for, but doesn't help other code. If the right answer is #if objectFile
, then that's a separate proposal, but I'd like us to consider what we want this code to look like.
Yeah, I think visibility control in particular would be easy to add and very appreciated by specific people.
This is one I would have said we could leave out, because we don't have existing underscored attributes to lead the way.
Doug