Skip to content

Commit 27254dd

Browse files
jyn514camelid
authored andcommitted
Add support for -Zunpretty=hir
Co-authored-by: Camelid <[email protected]>
1 parent e097b19 commit 27254dd

File tree

10 files changed

+111
-5
lines changed

10 files changed

+111
-5
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/Output.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface PaneWithCodeProps extends SimplePaneProps {
4646

4747
const Output: React.SFC = () => {
4848
const somethingToShow = useSelector(selectors.getSomethingToShow);
49-
const { meta: { focus }, execute, format, clippy, miri, macroExpansion, assembly, llvmIr, mir, wasm, gist } =
49+
const { meta: { focus }, execute, format, clippy, miri, macroExpansion, assembly, llvmIr, mir, hir, wasm, gist } =
5050
useSelector((state: State) => state.output);
5151

5252
const dispatch = useDispatch();
@@ -59,6 +59,7 @@ const Output: React.SFC = () => {
5959
const focusAssembly = useCallback(() => dispatch(actions.changeFocus(Focus.Asm)), [dispatch]);
6060
const focusLlvmIr = useCallback(() => dispatch(actions.changeFocus(Focus.LlvmIr)), [dispatch]);
6161
const focusMir = useCallback(() => dispatch(actions.changeFocus(Focus.Mir)), [dispatch]);
62+
const focusHir = useCallback(() => dispatch(actions.changeFocus(Focus.Hir)), [dispatch]);
6263
const focusWasm = useCallback(() => dispatch(actions.changeFocus(Focus.Wasm)), [dispatch]);
6364
const focusGist = useCallback(() => dispatch(actions.changeFocus(Focus.Gist)), [dispatch]);
6465

@@ -84,6 +85,7 @@ const Output: React.SFC = () => {
8485
{focus === Focus.Asm && <PaneWithCode {...assembly} kind="asm" />}
8586
{focus === Focus.LlvmIr && <PaneWithCode {...llvmIr} kind="llvm-ir" />}
8687
{focus === Focus.Mir && <PaneWithMir {...mir} kind="mir" />}
88+
{focus === Focus.Hir && <PaneWithMir {...hir} kind="hir" />}
8789
{focus === Focus.Wasm && <PaneWithCode {...wasm} kind="wasm" />}
8890
{focus === Focus.Gist && <Gist />}
8991
</div>
@@ -125,6 +127,10 @@ const Output: React.SFC = () => {
125127
label="MIR"
126128
onClick={focusMir}
127129
tabProps={mir} />
130+
<Tab kind={Focus.Hir} focus={focus}
131+
label="HIR"
132+
onClick={focusHir}
133+
tabProps={hir} />
128134
<Tab kind={Focus.Wasm} focus={focus}
129135
label="WASM"
130136
onClick={focusWasm}

ui/frontend/actions.ts

+30
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

@@ -790,6 +817,9 @@ export type Action =
790817
| ReturnType<typeof requestCompileMir>
791818
| ReturnType<typeof receiveCompileMirSuccess>
792819
| ReturnType<typeof receiveCompileMirFailure>
820+
| ReturnType<typeof requestCompileHir>
821+
| ReturnType<typeof receiveCompileHirSuccess>
822+
| ReturnType<typeof receiveCompileHirFailure>
793823
| ReturnType<typeof requestCompileWasm>
794824
| ReturnType<typeof receiveCompileWasmSuccess>
795825
| ReturnType<typeof receiveCompileWasmFailure>

ui/frontend/reducers/output/hir.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Action, ActionType } from '../../actions';
2+
import { finish, start } from './sharedStateManagement';
3+
4+
const DEFAULT: State = {
5+
requestsInProgress: 0,
6+
code: null,
7+
stdout: null,
8+
stderr: null,
9+
error: null,
10+
};
11+
12+
interface State {
13+
requestsInProgress: number;
14+
code?: string;
15+
stdout?: string;
16+
stderr?: string;
17+
error?: string;
18+
}
19+
20+
export default function hir(state = DEFAULT, action: Action) {
21+
switch (action.type) {
22+
case ActionType.CompileHirRequest:
23+
return start(DEFAULT, state);
24+
case ActionType.CompileHirSucceeded: {
25+
const { code = '', stdout = '', stderr = '' } = action;
26+
return finish(state, { code, stdout, stderr });
27+
}
28+
case ActionType.CompileHirFailed:
29+
return finish(state, { error: action.error });
30+
default:
31+
return state;
32+
}
33+
}

ui/frontend/reducers/output/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import clippy from './clippy';
55
import execute from './execute';
66
import format from './format';
77
import gist from './gist';
8+
import hir from './hir';
89
import llvmIr from './llvmIr';
10+
import macroExpansion from './macroExpansion';
911
import meta from './meta';
1012
import mir from './mir';
1113
import miri from './miri';
12-
import macroExpansion from './macroExpansion';
1314
import wasm from './wasm';
1415

1516
const output = combineReducers({
@@ -21,6 +22,7 @@ const output = combineReducers({
2122
assembly,
2223
llvmIr,
2324
mir,
25+
hir,
2426
wasm,
2527
execute,
2628
gist,

ui/frontend/reducers/output/meta.ts

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default function meta(state = DEFAULT, action: Action) {
2929
case ActionType.CompileMirRequest:
3030
return { ...state, focus: Focus.Mir };
3131

32+
case ActionType.CompileHirRequest:
33+
return { ...state, focus: Focus.Hir };
34+
3235
case ActionType.CompileWasmRequest:
3336
return { ...state, focus: Focus.Wasm };
3437

ui/frontend/selectors/index.ts

+5-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;
@@ -142,6 +145,7 @@ const getOutputs = (state: State) => [
142145
state.output.gist,
143146
state.output.llvmIr,
144147
state.output.mir,
148+
state.output.hir,
145149
state.output.miri,
146150
state.output.macroExpansion,
147151
state.output.wasm,

ui/frontend/types.ts

+2
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',
@@ -108,6 +109,7 @@ export enum Focus {
108109
MacroExpansion = 'macro-expansion',
109110
LlvmIr = 'llvm-ir',
110111
Mir = 'mir',
112+
Hir = 'hir',
111113
Wasm = 'wasm',
112114
Asm = 'asm',
113115
Execute = 'execute',

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)