Skip to content

Commit f6dde9b

Browse files
committed
Initial
1 parent 524d2aa commit f6dde9b

File tree

1 file changed

+142
-59
lines changed

1 file changed

+142
-59
lines changed

text/0000-build-target-edition.md

Lines changed: 142 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,180 @@
1-
- Feature Name: (fill me in with a unique ident, `my_awesome_feature`)
2-
- Start Date: (fill me in with today's date, YYYY-MM-DD)
1+
- Feature Name: `build-target-edition`
2+
- Start Date: 2025-02-13
33
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000)
44
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000)
55

66
# Summary
77
[summary]: #summary
88

9-
One paragraph explanation of the feature.
9+
Deprecate `lib.edition`, etc in favor of only setting `package.edition`, removing the fields in the next Edition.
1010

1111
# Motivation
1212
[motivation]: #motivation
1313

14-
Why are we doing this? What use cases does it support? What is the expected outcome?
14+
Cargo supports setting the edition per-build-target:
15+
```toml
16+
[package]
17+
name = "foo"
18+
edition = "2021"
19+
20+
[lib]
21+
edition = "2015"
22+
23+
[[bin]]
24+
name = "foo"
25+
path = "src/main.rs"
26+
edition = "2015"
27+
28+
[[example]]
29+
name = "foo"
30+
path = "examples/foo.rs"
31+
edition = "2015"
32+
33+
[[test]]
34+
name = "foo"
35+
path = "tests/foo.rs"
36+
edition = "2015"
37+
38+
[[bench]]
39+
name = "foo"
40+
path = "benches/foo.rs"
41+
edition = "2015"
42+
```
43+
44+
This was intended for ([cargo#5661](https://github.com/rust-lang/cargo/issues/5661)):
45+
- Migrating to a new edition per build-target
46+
- Per edition tests
47+
48+
In practice, this feature does not seem to be in common use.
49+
Searching the latest `Cargo.toml` files of every package on crates.io,
50+
we found 13 packages using this feature
51+
([zulip](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Deprecate.20build-target.20.60edition.60.20field.3F/near/499047806)):
52+
- 4 set `edition` on the sole build-target, rather than on the `package`
53+
- 3 set `edition` because they enumerated every build-target field but then forgot to update them when updating `package.edition`
54+
- 3 (+2 forks) have per-edition tests
55+
- 1 has every version yanked
56+
57+
While this does not account for transient use of this feature during an Edition migration,
58+
from our experience and observing others, we think this practice is not very common.
59+
In fact, it seems more likely that migrating a lint at a time may be more beneficial
60+
([cargo#11125](https://github.com/rust-lang/cargo/issues/11125#issuecomment-2641119791)).
61+
There is also an open question on the Cargo team on how much to focus on multiple build-targets per package
62+
vs individual packages
63+
(see [This Development-cycle in Cargo: 1.77](https://blog.rust-lang.org/inside-rust/2024/02/13/this-development-cycle-in-cargo-1-77.html#when-to-use-packages-or-workspaces)).
64+
65+
Drawbacks of this feature include:
66+
- Using this has a lot of friction as users have to explicitly
67+
enumerate each build target they want to set `edition` on which usually requires
68+
also setting the `name` and `path`.
69+
- This has led to bugs where people thought they migrated editions but did not
70+
- This is an easily overlooked feature to take into account when extending Cargo
71+
- Cargo cannot tell a `build.rs` what Edition to generate code for as it may generate code for one of several
72+
([cargo#6408](https://github.com/rust-lang/cargo/issues/6408)).
73+
This will become more important once we have [metabuild](https://github.com/rust-lang/cargo/issues/14903) for delegating build scripts to dependencies.
74+
- Making it more difficult for tools like `cargo fmt`
75+
([rustfmt#5071](https://github.com/rust-lang/rustfmt/pull/5071)) which need to map
76+
a file back to its edition which requires heuristics to associate a `.rs`
77+
file with a `Cargo.toml` and then to associate it with a specific build-target.
1578

1679
# Guide-level explanation
1780
[guide-level-explanation]: #guide-level-explanation
1881

19-
Explain the proposal as if it was already included in the language and you were teaching it to another Rust programmer. That generally means:
82+
Documentation updates:
2083

21-
- Introducing new named concepts.
22-
- Explaining the feature largely in terms of examples.
23-
- Explaining how Rust programmers should *think* about the feature, and how it should impact the way they use Rust. It should explain the impact as concretely as possible.
24-
- If applicable, provide sample error messages, deprecation warnings, or migration guidance.
25-
- If applicable, describe the differences between teaching this to existing Rust programmers and new Rust programmers.
26-
- Discuss how this impacts the ability to read, understand, and maintain Rust code. Code is read and modified far more often than written; will the proposed feature make code easier to maintain?
84+
## Configuring a target
2785

28-
For implementation-oriented RFCs (e.g. for compiler internals), this section should focus on how compiler contributors should think about the change, and give examples of its concrete impact. For policy RFCs, this section should provide an example-driven introduction to the policy, and explain its impact in concrete terms.
86+
*From the [Cargo book](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#configuring-a-target)*
2987

30-
# Reference-level explanation
31-
[reference-level-explanation]: #reference-level-explanation
88+
...
89+
90+
```toml
91+
[lib]
92+
name = "foo" # The name of the target.
93+
path = "src/lib.rs" # The source file of the target.
94+
test = true # Is tested by default.
95+
doctest = true # Documentation examples are tested by default.
96+
bench = true # Is benchmarked by default.
97+
doc = true # Is documented by default.
98+
proc-macro = false # Set to `true` for a proc-macro library.
99+
harness = true # Use libtest harness.
100+
crate-type = ["lib"] # The crate types to generate.
101+
required-features = [] # Features required to build this target (N/A for lib).
102+
103+
edition = "2015" # Deprecated, N/A for Edition 20XX+
104+
```
105+
106+
...
107+
108+
### The `edition` field
109+
110+
The `edition` field defines the [Rust edition] the target will use. If not
111+
specified, it defaults to the [`edition` field][package-edition] for the
112+
`[package]`.
113+
114+
This field is deprecated and unsupported for Edition 20XX+
115+
116+
## Migration guide
117+
118+
*From [Rust Edition Guide: Advanced migration strategies](https://doc.rust-lang.org/nightly/edition-guide/editions/advanced-migrations.html#migrating-a-large-project-or-workspace)*
119+
120+
### Migrating a large project or workspace
121+
122+
You can migrate a large project incrementally to make the process easier if you run into problems.
123+
124+
In a [Cargo workspace], each package defines its own edition, so the process naturally involves migrating one package at a time.
125+
126+
Within a [Cargo package], you can either migrate the entire package at once, or migrate individual [Cargo targets] one at a time.
127+
For example, if you have multiple binaries, tests, and examples, you can use specific target selection flags with `cargo fix --edition` to migrate just that one target.
128+
By default, `cargo fix` uses `--all-targets`.
129+
130+
*(removed talk of the build-target `edition` field)*
32131

33-
This is the technical portion of the RFC. Explain the design in sufficient detail that:
132+
### Migrating macros
34133

35-
- Its interaction with other features is clear.
36-
- It is reasonably clear how the feature would be implemented.
37-
- Corner cases are dissected by example.
134+
...
38135

39-
The section should return to the examples given in the previous section, and explain more fully how the detailed proposal makes those examples work.
136+
If you have macros, you are encouraged to make sure you have tests that fully
137+
cover the macro's syntax. You may also want to test the macros by importing and
138+
using them in crates from multiple editions, just to ensure it works correctly
139+
everywhere.
140+
You can do this in doctests by setting the [edition attribute](https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#attributes)
141+
or by creating a package for your tests in your workspace for each edition.
142+
143+
If you run into issues, you'll need to read through the chapters of
144+
this guide to understand how the code can be changed to work across all
145+
editions.
146+
147+
*(added a testing strategy which was previously left unspoken)*
148+
149+
# Reference-level explanation
150+
[reference-level-explanation]: #reference-level-explanation
151+
152+
A non-`None` edition will be considered deprecated
153+
- A deprecation message will eventually be shown by Cargo
154+
- Timing depends on if this will be blocked on having `[lints]` control over this or not
155+
- In Edition 20XX+, an error will be reported when this field is present
40156

41157
# Drawbacks
42158
[drawbacks]: #drawbacks
43159

44-
Why should we *not* do this?
160+
- This makes testing macros more difficult as they are limited to either
161+
- doctests
162+
- creating packages just for the sake of defining tests
45163

46164
# Rationale and alternatives
47165
[rationale-and-alternatives]: #rationale-and-alternatives
48166

49-
- Why is this design the best in the space of possible designs?
50-
- What other designs have been considered and what is the rationale for not choosing them?
51-
- What is the impact of not doing this?
52-
- If this is a language proposal, could this be done in a library or macro instead? Does the proposed change make Rust code easier or harder to read, understand, and maintain?
53-
54167
# Prior art
55168
[prior-art]: #prior-art
56169

57-
Discuss prior art, both the good and the bad, in relation to this proposal.
58-
A few examples of what this can include are:
59-
60-
- For language, library, cargo, tools, and compiler proposals: Does this feature exist in other programming languages and what experience have their community had?
61-
- For community proposals: Is this done by some other community and what were their experiences with it?
62-
- For other teams: What lessons can we learn from what other communities have done here?
63-
- Papers: Are there any published papers or great posts that discuss this? If you have some relevant papers to refer to, this can serve as a more detailed theoretical background.
64-
65-
This section is intended to encourage you as an author to think about the lessons from other languages, provide readers of your RFC with a fuller picture.
66-
If there is no prior art, that is fine - your ideas are interesting to us whether they are brand new or if it is an adaptation from other languages.
67-
68-
Note that while precedent set by other languages is some motivation, it does not on its own motivate an RFC.
69-
Please also take into consideration that rust sometimes intentionally diverges from common language features.
70-
71170
# Unresolved questions
72171
[unresolved-questions]: #unresolved-questions
73172

74-
- What parts of the design do you expect to resolve through the RFC process before this gets merged?
75-
- What parts of the design do you expect to resolve through the implementation of this feature before stabilization?
76-
- What related issues do you consider out of scope for this RFC that could be addressed in the future independently of the solution that comes out of this RFC?
173+
- When will Cargo start to report the deprecation message?
174+
- Cargo currently lacks lint control for itself ([cargo#12235](https://github.com/rust-lang/cargo/issues/12235)) which we could wait for
175+
- We could unconditionally report the warning but the Cargo team avoids adding warnings without a way of suppressing them without changing behavior
77176

78177
# Future possibilities
79178
[future-possibilities]: #future-possibilities
80179

81-
Think about what the natural extension and evolution of your proposal would
82-
be and how it would affect the language and project as a whole in a holistic
83-
way. Try to use this section as a tool to more fully consider all possible
84-
interactions with the project and language in your proposal.
85-
Also consider how this all fits into the roadmap for the project
86-
and of the relevant sub-team.
87-
88-
This is also a good place to "dump ideas", if they are out of scope for the
89-
RFC you are writing but otherwise related.
90-
91-
If you have tried and cannot think of any future possibilities,
92-
you may simply state that you cannot think of anything.
93-
94-
Note that having something written down in the future-possibilities section
95-
is not a reason to accept the current or a future RFC; such notes should be
96-
in the section on motivation or rationale in this or subsequent RFCs.
97-
The section merely provides additional information.
180+
- Reporting the Edition to `build.rs`

0 commit comments

Comments
 (0)