Skip to content

Commit 489a2e0

Browse files
jyn514camelid
authored andcommitted
Add support for -Zunpretty=hir
Co-authored-by: Camelid <[email protected]>
1 parent 04bd4bf commit 489a2e0

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

ui/frontend/BuildMenu.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const useDispatchAndClose = (action: () => void, close: () => void) => {
2424
}
2525

2626
const BuildMenu: React.SFC<BuildMenuProps> = props => {
27+
const isHirAvailable = useSelector(selectors.isHirAvailable);
2728
const isWasmAvailable = useSelector(selectors.isWasmAvailable);
2829

2930
const compile = useDispatchAndClose(actions.performCompile, props.close);
3031
const compileToAssembly = useDispatchAndClose(actions.performCompileToAssembly, props.close);
3132
const compileToLLVM = useDispatchAndClose(actions.performCompileToLLVM, props.close);
3233
const compileToMir = useDispatchAndClose(actions.performCompileToMir, props.close);
34+
const compileToHir = useDispatchAndClose(actions.performCompileToNightlyHir, props.close);
3335
const compileToWasm = useDispatchAndClose(actions.performCompileToNightlyWasm, props.close);
3436
const execute = useDispatchAndClose(actions.performExecute, props.close);
3537
const test = useDispatchAndClose(actions.performTest, props.close);
@@ -55,7 +57,11 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
5557
Build and show the resulting LLVM IR, LLVM’s intermediate representation.
5658
</ButtonMenuItem>
5759
<ButtonMenuItem name="MIR" onClick={compileToMir}>
58-
Build and show the resulting MIR, Rust’s intermediate representation.
60+
Build and show the resulting MIR, Rust’s control-flow-based intermediate representation.
61+
</ButtonMenuItem>
62+
<ButtonMenuItem name="HIR" onClick={compileToHir}>
63+
Build and show the resulting HIR, Rust’s syntax-based intermediate representation.
64+
{!isHirAvailable && <HirAside />}
5965
</ButtonMenuItem>
6066
<ButtonMenuItem name="WASM" onClick={compileToWasm}>
6167
Build a WebAssembly module for web browsers, in the .WAT textual representation.
@@ -65,6 +71,13 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
6571
);
6672
};
6773

74+
const HirAside: React.SFC = () => (
75+
<p className="build-menu__aside">
76+
Note: HIR currently requires using the Nightly channel, selecting this
77+
option will switch to Nightly.
78+
</p>
79+
);
80+
6881
const WasmAside: React.SFC = () => (
6982
<p className="build-menu__aside">
7083
Note: WASM currently requires using the Nightly channel, selecting this

ui/frontend/actions.ts

+27
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export enum ActionType {
8383
CompileLlvmIrRequest = 'COMPILE_LLVM_IR_REQUEST',
8484
CompileLlvmIrSucceeded = 'COMPILE_LLVM_IR_SUCCEEDED',
8585
CompileLlvmIrFailed = 'COMPILE_LLVM_IR_FAILED',
86+
CompileHirRequest = 'COMPILE_HIR_REQUEST',
87+
CompileHirSucceeded = 'COMPILE_HIR_SUCCEEDED',
88+
CompileHirFailed = 'COMPILE_HIR_FAILED',
8689
CompileMirRequest = 'COMPILE_MIR_REQUEST',
8790
CompileMirSucceeded = 'COMPILE_MIR_SUCCEEDED',
8891
CompileMirFailed = 'COMPILE_MIR_FAILED',
@@ -346,6 +349,27 @@ const performCompileToLLVMOnly = () =>
346349
failure: receiveCompileLlvmIrFailure,
347350
});
348351

352+
const requestCompileHir = () =>
353+
createAction(ActionType.CompileHirRequest);
354+
355+
const receiveCompileHirSuccess = ({ code, stdout, stderr }) =>
356+
createAction(ActionType.CompileHirSucceeded, { code, stdout, stderr });
357+
358+
const receiveCompileHirFailure = ({ error }) =>
359+
createAction(ActionType.CompileHirFailed, { error });
360+
361+
const performCompileToHirOnly = () =>
362+
performCompileShow('hir', {
363+
request: requestCompileHir,
364+
success: receiveCompileHirSuccess,
365+
failure: receiveCompileHirFailure,
366+
});
367+
368+
const performCompileToNightlyHirOnly = (): ThunkAction => dispatch => {
369+
dispatch(changeChannel(Channel.Nightly));
370+
dispatch(performCompileToHirOnly());
371+
};
372+
349373
const requestCompileMir = () =>
350374
createAction(ActionType.CompileMirRequest);
351375

@@ -390,6 +414,7 @@ const PRIMARY_ACTIONS: { [index in PrimaryAction]: () => ThunkAction } = {
390414
[PrimaryActionCore.Test]: performTestOnly,
391415
[PrimaryActionAuto.Auto]: performAutoOnly,
392416
[PrimaryActionCore.LlvmIr]: performCompileToLLVMOnly,
417+
[PrimaryActionCore.Hir]: performCompileToHirOnly,
393418
[PrimaryActionCore.Mir]: performCompileToMirOnly,
394419
[PrimaryActionCore.Wasm]: performCompileToNightlyWasmOnly,
395420
};
@@ -417,6 +442,8 @@ export const performCompileToLLVM =
417442
performAndSwitchPrimaryAction(performCompileToLLVMOnly, PrimaryActionCore.LlvmIr);
418443
export const performCompileToMir =
419444
performAndSwitchPrimaryAction(performCompileToMirOnly, PrimaryActionCore.Mir);
445+
export const performCompileToNightlyHir =
446+
performAndSwitchPrimaryAction(performCompileToNightlyHirOnly, PrimaryActionCore.Hir);
420447
export const performCompileToNightlyWasm =
421448
performAndSwitchPrimaryAction(performCompileToNightlyWasmOnly, PrimaryActionCore.Wasm);
422449

ui/frontend/selectors/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const LABELS: { [index in PrimaryActionCore]: string } = {
7171
[PrimaryActionCore.Compile]: 'Build',
7272
[PrimaryActionCore.Execute]: 'Run',
7373
[PrimaryActionCore.LlvmIr]: 'Show LLVM IR',
74+
[PrimaryActionCore.Hir]: 'Show HIR',
7475
[PrimaryActionCore.Mir]: 'Show MIR',
7576
[PrimaryActionCore.Test]: 'Test',
7677
[PrimaryActionCore.Wasm]: 'Show WASM',
@@ -102,9 +103,11 @@ export const miriVersionDetailsText = createSelector([getMiri], versionDetails);
102103

103104
const editionSelector = (state: State) => state.configuration.edition;
104105

105-
export const isWasmAvailable = (state: State) => (
106+
export const isNightlyChannel = (state: State) => (
106107
state.configuration.channel === Channel.Nightly
107108
);
109+
export const isWasmAvailable = isNightlyChannel;
110+
export const isHirAvailable = isNightlyChannel;
108111

109112
export const getModeLabel = (state: State) => {
110113
const { configuration: { mode } } = state;

ui/frontend/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export enum PrimaryActionCore {
7474
Compile = 'compile',
7575
Execute = 'execute',
7676
LlvmIr = 'llvm-ir',
77+
Hir = 'hir',
7778
Mir = 'mir',
7879
Test = 'test',
7980
Wasm = 'wasm',

ui/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ fn parse_target(s: &str) -> Result<sandbox::CompileTarget> {
934934
sandbox::ProcessAssembly::Filter),
935935
"llvm-ir" => sandbox::CompileTarget::LlvmIr,
936936
"mir" => sandbox::CompileTarget::Mir,
937+
"hir" => sandbox::CompileTarget::Hir,
937938
"wasm" => sandbox::CompileTarget::Wasm,
938939
value => InvalidTarget { value }.fail()?,
939940
})

ui/src/sandbox.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ impl Sandbox {
169169
if process == ProcessAssembly::Filter {
170170
code = super::asm_cleanup::filter_asm(&code);
171171
}
172+
} else if CompileTarget::Hir == req.target {
173+
// TODO: Run rustfmt on the generated HIR.
172174
}
173175

174176
Ok(CompileResponse {
@@ -479,7 +481,13 @@ fn build_execution_command(target: Option<CompileTarget>, channel: Channel, mode
479481
}
480482

481483
if let Some(target) = target {
482-
cmd.extend(&["--", "-o", "/playground-result/compilation"]);
484+
cmd.extend(&["--", "-o"]);
485+
if target == Hir {
486+
// -Zunpretty=hir only emits the HIR, not the binary itself
487+
cmd.push("/playground-result/compilation.hir");
488+
} else {
489+
cmd.push("/playground-result/compilation");
490+
}
483491

484492
match target {
485493
Assembly(flavor, _, _) => {
@@ -501,6 +509,7 @@ fn build_execution_command(target: Option<CompileTarget>, channel: Channel, mode
501509
},
502510
LlvmIr => cmd.push("--emit=llvm-ir"),
503511
Mir => cmd.push("--emit=mir"),
512+
Hir => cmd.push("-Zunpretty=hir"),
504513
Wasm => { /* handled by cargo-wasm wrapper */ },
505514
}
506515
}
@@ -612,6 +621,7 @@ pub enum CompileTarget {
612621
Assembly(AssemblyFlavor, DemangleAssembly, ProcessAssembly),
613622
LlvmIr,
614623
Mir,
624+
Hir,
615625
Wasm,
616626
}
617627

@@ -621,6 +631,7 @@ impl CompileTarget {
621631
CompileTarget::Assembly(_, _, _) => "s",
622632
CompileTarget::LlvmIr => "ll",
623633
CompileTarget::Mir => "mir",
634+
CompileTarget::Hir => "hir",
624635
CompileTarget::Wasm => "wat",
625636
};
626637
OsStr::new(ext)
@@ -635,6 +646,7 @@ impl fmt::Display for CompileTarget {
635646
Assembly(_, _, _) => "assembly".fmt(f),
636647
LlvmIr => "LLVM IR".fmt(f),
637648
Mir => "Rust MIR".fmt(f),
649+
Hir => "Rust HIR".fmt(f),
638650
Wasm => "WebAssembly".fmt(f),
639651
}
640652
}

0 commit comments

Comments
 (0)