Skip to content

Commit a24706f

Browse files
committed
Support Rust edition 2024
It's nightly-only for now, but available.
1 parent ea606ca commit a24706f

File tree

10 files changed

+95
-7
lines changed

10 files changed

+95
-7
lines changed

compiler/base/modify-cargo-toml/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ fn ensure_string_in_vec(values: &mut Vec<String>, val: &str) {
2626
}
2727
}
2828

29+
pub fn set_feature_edition2024(cargo_toml: Value) -> Value {
30+
#[derive(Debug, Serialize, Deserialize)]
31+
#[serde(rename_all = "kebab-case")]
32+
struct CargoToml {
33+
#[serde(default)]
34+
cargo_features: Vec<String>,
35+
#[serde(flatten)]
36+
other: Other,
37+
}
38+
39+
modify(cargo_toml, |mut cargo_toml: CargoToml| {
40+
ensure_string_in_vec(&mut cargo_toml.cargo_features, "edition2024");
41+
cargo_toml
42+
})
43+
}
44+
2945
pub fn set_edition(cargo_toml: Value, edition: &str) -> Value {
3046
#[derive(Debug, Serialize, Deserialize)]
3147
#[serde(rename_all = "kebab-case")]

compiler/base/modify-cargo-toml/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ fn main() {
1919
let mut cargo_toml: Value = toml::from_str(&input)
2020
.unwrap_or_else(|e| panic!("Cannot parse {} as TOML: {}", input_filename.display(), e));
2121

22+
if env::var_os("PLAYGROUND_FEATURE_EDITION2024").is_some() {
23+
cargo_toml = set_feature_edition2024(cargo_toml);
24+
}
25+
2226
if let Ok(edition) = env::var("PLAYGROUND_EDITION") {
2327
cargo_toml = set_edition(cargo_toml, &edition);
2428
}

compiler/base/orchestrator/src/coordinator.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,24 @@ pub enum Edition {
9191
Rust2015,
9292
Rust2018,
9393
Rust2021,
94+
Rust2024,
9495
}
9596

9697
impl Edition {
9798
#[cfg(test)]
98-
pub(crate) const ALL: [Self; 3] = [Self::Rust2015, Self::Rust2018, Self::Rust2021];
99+
pub(crate) const ALL: [Self; 4] = [
100+
Self::Rust2015,
101+
Self::Rust2018,
102+
Self::Rust2021,
103+
Self::Rust2024,
104+
];
99105

100106
pub(crate) fn to_str(self) -> &'static str {
101107
match self {
102108
Edition::Rust2015 => "2015",
103109
Edition::Rust2018 => "2018",
104110
Edition::Rust2021 => "2021",
111+
Edition::Rust2024 => "2024",
105112
}
106113
}
107114

@@ -221,6 +228,10 @@ impl ExecuteRequest {
221228

222229
impl CargoTomlModifier for ExecuteRequest {
223230
fn modify_cargo_toml(&self, mut cargo_toml: toml::Value) -> toml::Value {
231+
if self.edition == Edition::Rust2024 {
232+
cargo_toml = modify_cargo_toml::set_feature_edition2024(cargo_toml);
233+
}
234+
224235
cargo_toml = modify_cargo_toml::set_edition(cargo_toml, self.edition.to_cargo_toml_key());
225236

226237
if let Some(crate_type) = self.crate_type.to_library_cargo_toml_key() {
@@ -321,6 +332,10 @@ impl CompileRequest {
321332

322333
impl CargoTomlModifier for CompileRequest {
323334
fn modify_cargo_toml(&self, mut cargo_toml: toml::Value) -> toml::Value {
335+
if self.edition == Edition::Rust2024 {
336+
cargo_toml = modify_cargo_toml::set_feature_edition2024(cargo_toml);
337+
}
338+
324339
cargo_toml = modify_cargo_toml::set_edition(cargo_toml, self.edition.to_cargo_toml_key());
325340

326341
if let Some(crate_type) = self.crate_type.to_library_cargo_toml_key() {
@@ -1526,8 +1541,16 @@ mod tests {
15261541
#[snafu::report]
15271542
async fn execute_edition() -> Result<()> {
15281543
let params = [
1529-
(r#"fn x() { let dyn = true; }"#, [true, false, false]),
1530-
(r#"fn x() { u16::try_from(1u8); }"#, [false, false, true]),
1544+
(r#"fn x() { let dyn = true; }"#, [true, false, false, false]),
1545+
(
1546+
r#"fn x() { u16::try_from(1u8); }"#,
1547+
[false, false, true, true],
1548+
),
1549+
(
1550+
r#"#![feature(gen_blocks)]
1551+
fn x() { gen { yield 1u8 }; }"#,
1552+
[false, false, false, true],
1553+
),
15311554
];
15321555

15331556
let tests = params.into_iter().flat_map(|(code, works_in)| {

tests/spec/features/editions_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@
3737
end
3838
end
3939

40+
scenario "using the 2024 edition" do
41+
editor.set <<-EOF
42+
#![feature(gen_blocks)]
43+
44+
fn main() {
45+
let mut x = gen { yield 1 };
46+
47+
eprintln!("{:?}", x.next());
48+
eprintln!("{:?}", x.next());
49+
}
50+
EOF
51+
52+
in_advanced_options_menu { select '2024' }
53+
click_on("Run")
54+
55+
within(:output, :stderr) do
56+
expect(page).to have_content 'Some(1)'
57+
expect(page).to have_content 'None'
58+
end
59+
end
60+
4061
def editor
4162
Editor.new(page)
4263
end

ui/frontend/AdvancedOptionsMenu.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Either as EitherConfig, Select as SelectConfig } from './ConfigElement'
66
import MenuGroup from './MenuGroup';
77
import { State } from './reducers';
88
import * as selectors from './selectors';
9-
import { Backtrace, Edition } from './types';
9+
import { Backtrace, Channel, Edition } from './types';
1010

1111
const AdvancedOptionsMenu: React.FC = () => {
1212
const isEditionDefault = useSelector(selectors.isEditionDefault);
@@ -19,6 +19,9 @@ const AdvancedOptionsMenu: React.FC = () => {
1919
const changeEdition = useCallback((e: Edition) => dispatch(actions.changeEdition(e)), [dispatch]);
2020
const changeBacktrace = useCallback((b: Backtrace) => dispatch(actions.changeBacktrace(b)), [dispatch]);
2121

22+
const channel = useSelector((state: State) => state.configuration.channel);
23+
const switchText = (channel !== Channel.Nightly) ? ' (will select nightly Rust)' : '';
24+
2225
return (
2326
<MenuGroup title="Advanced options">
2427
<SelectConfig
@@ -30,6 +33,7 @@ const AdvancedOptionsMenu: React.FC = () => {
3033
<option value={Edition.Rust2015}>2015</option>
3134
<option value={Edition.Rust2018}>2018</option>
3235
<option value={Edition.Rust2021}>2021</option>
36+
<option value={Edition.Rust2024}>2024{switchText}</option>
3337
</SelectConfig>
3438

3539
<EitherConfig

ui/frontend/actions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,17 @@ export const changeChannel = (channel: Channel) =>
155155
export const changeMode = (mode: Mode) =>
156156
createAction(ActionType.ChangeMode, { mode });
157157

158-
export const changeEdition = (edition: Edition) =>
158+
const changeEditionRaw = (edition: Edition) =>
159159
createAction(ActionType.ChangeEdition, { edition });
160160

161+
export const changeEdition = (edition: Edition): ThunkAction => dispatch => {
162+
if (edition === Edition.Rust2024) {
163+
dispatch(changeChannel(Channel.Nightly));
164+
}
165+
166+
dispatch(changeEditionRaw(edition));
167+
}
168+
161169
export const changeBacktrace = (backtrace: Backtrace) =>
162170
createAction(ActionType.ChangeBacktrace, { backtrace });
163171

@@ -561,6 +569,8 @@ function parseEdition(s?: string): Edition | null {
561569
return Edition.Rust2018;
562570
case '2021':
563571
return Edition.Rust2021;
572+
case '2024':
573+
return Edition.Rust2024;
564574
default:
565575
return null;
566576
}
@@ -599,7 +609,7 @@ export function indexPageLoad({
599609

600610
dispatch(changeChannel(channel));
601611
dispatch(changeMode(mode));
602-
dispatch(changeEdition(edition));
612+
dispatch(changeEditionRaw(edition));
603613
};
604614
}
605615

@@ -622,7 +632,7 @@ export type Action =
622632
| ReturnType<typeof changeBacktrace>
623633
| ReturnType<typeof changeChannel>
624634
| ReturnType<typeof changeDemangleAssembly>
625-
| ReturnType<typeof changeEdition>
635+
| ReturnType<typeof changeEditionRaw>
626636
| ReturnType<typeof changeEditor>
627637
| ReturnType<typeof changeFocus>
628638
| ReturnType<typeof changeKeybinding>

ui/frontend/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export enum Edition {
9898
Rust2015 = '2015',
9999
Rust2018 = '2018',
100100
Rust2021 = '2021',
101+
Rust2024 = '2024',
101102
}
102103

103104
export enum Backtrace {

ui/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ fn parse_edition(s: &str) -> Result<Option<sandbox::Edition>> {
548548
"2015" => Some(sandbox::Edition::Rust2015),
549549
"2018" => Some(sandbox::Edition::Rust2018),
550550
"2021" => Some(sandbox::Edition::Rust2021),
551+
"2024" => Some(sandbox::Edition::Rust2024),
551552
value => InvalidEditionSnafu { value }.fail()?,
552553
})
553554
}

ui/src/sandbox.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ pub enum Edition {
565565
Rust2015,
566566
Rust2018,
567567
Rust2021, // TODO - add parallel tests for 2021
568+
Rust2024,
568569
}
569570

570571
impl Edition {
@@ -575,6 +576,7 @@ impl Edition {
575576
Rust2015 => "2015",
576577
Rust2018 => "2018",
577578
Rust2021 => "2021",
579+
Rust2024 => "2024",
578580
}
579581
}
580582
}
@@ -639,6 +641,10 @@ impl DockerCommandExt for Command {
639641

640642
fn apply_edition(&mut self, req: impl EditionRequest) {
641643
if let Some(edition) = req.edition() {
644+
if edition == Edition::Rust2024 {
645+
self.args(&["--env", &format!("PLAYGROUND_FEATURE_EDITION2024=true")]);
646+
}
647+
642648
self.args(&[
643649
"--env",
644650
&format!("PLAYGROUND_EDITION={}", edition.cargo_ident()),
@@ -799,6 +805,7 @@ mod sandbox_orchestrator_integration_impls {
799805
coordinator::Edition::Rust2015 => super::Edition::Rust2015,
800806
coordinator::Edition::Rust2018 => super::Edition::Rust2018,
801807
coordinator::Edition::Rust2021 => super::Edition::Rust2021,
808+
coordinator::Edition::Rust2024 => super::Edition::Rust2024,
802809
}
803810
}
804811
}

ui/src/server_axum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ pub(crate) mod api_orchestrator_integration_impls {
11271127
"2015" => Edition::Rust2015,
11281128
"2018" => Edition::Rust2018,
11291129
"2021" => Edition::Rust2021,
1130+
"2024" => Edition::Rust2024,
11301131
value => return ParseEditionSnafu { value }.fail(),
11311132
})
11321133
}

0 commit comments

Comments
 (0)