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

Allow @_section for static non-global data #73218

Open
saagarjha opened this issue Apr 24, 2024 · 0 comments
Open

Allow @_section for static non-global data #73218

saagarjha opened this issue Apr 24, 2024 · 0 comments
Labels
feature A feature request or implementation triage needed This issue needs more specific labels

Comments

@saagarjha
Copy link
Contributor

Motivation

The experimental @_section attribute relocates a variable into a specific section, with some constraints. Notably, the current implementation requires that the expression be applied to global functions and variables. This is not the case in other languages such as C or C++, where it can be applied to a number of constructs that are guaranteed to only have a single definition. It would be nice if Swift could also support this where appropriate.

Proposed solution

Allow @_section in more places. For example, this should be well-formed:

struct Foo {
	@_section("__DATA,__foobar")
	static let bar = 0
}

There is only going to be one of these in the entire program, so this should be technically feasible. And by allowing this it means that bar doesn't have to be a global but can be scoped as appropriate to where it makes sense in the program. Of note, this means that it can be generated from a macro, which cannot inject expressions into the global scope.

Alternatives considered

There's not much else you can do here while maintaining the spirit of the request

Additional information

I am working on a logging library very similar to os_log, which means I need to stick strings into a specific section in the binary using a macro. The Swift compiler has a hard-coded feature to move os_log strings into its own section. However, with this feature I can do this myself from a normal macro.

@saagarjha saagarjha added feature A feature request or implementation triage needed This issue needs more specific labels labels Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant