From f1c14ee3d7bd3ff5f364e73a9ac9e4c9e4d034b0 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 21 Feb 2021 12:34:10 -0500 Subject: [PATCH] Add support for -Zunpretty=hir --- ui/frontend/BuildMenu.tsx | 18 +++++++++++++++--- ui/frontend/actions.ts | 27 +++++++++++++++++++++++++++ ui/frontend/selectors/index.ts | 3 ++- ui/frontend/types.ts | 1 + ui/src/main.rs | 1 + ui/src/sandbox.rs | 14 +++++++++++++- 6 files changed, 59 insertions(+), 5 deletions(-) diff --git a/ui/frontend/BuildMenu.tsx b/ui/frontend/BuildMenu.tsx index 10f63ae6f..1e58b3baf 100644 --- a/ui/frontend/BuildMenu.tsx +++ b/ui/frontend/BuildMenu.tsx @@ -24,12 +24,13 @@ const useDispatchAndClose = (action: () => void, close: () => void) => { } const BuildMenu: React.SFC = props => { - const isWasmAvailable = useSelector(selectors.isWasmAvailable); + const isNightlyAvailable = useSelector(selectors.isNightlyChannel); const compile = useDispatchAndClose(actions.performCompile, props.close); const compileToAssembly = useDispatchAndClose(actions.performCompileToAssembly, props.close); const compileToLLVM = useDispatchAndClose(actions.performCompileToLLVM, props.close); const compileToMir = useDispatchAndClose(actions.performCompileToMir, props.close); + const compileToHir = useDispatchAndClose(actions.performCompileToNightlyHir, props.close); const compileToWasm = useDispatchAndClose(actions.performCompileToNightlyWasm, props.close); const execute = useDispatchAndClose(actions.performExecute, props.close); const test = useDispatchAndClose(actions.performTest, props.close); @@ -55,16 +56,27 @@ const BuildMenu: React.SFC = props => { Build and show the resulting LLVM IR, LLVM’s intermediate representation. - Build and show the resulting MIR, Rust’s intermediate representation. + Build and show the resulting MIR, Rust’s intermediate representation for control flow. + + + Build and show the resulting HIR, Rust’s intermediate representation for items in the current crate. + {!isNightlyAvailable && } Build a WebAssembly module for web browsers, in the .WAT textual representation. - {!isWasmAvailable && } + {!isNightlyAvailable && } ); }; +const HirAside: React.SFC = () => ( +

+ Note: HIR currently requires using the Nightly channel, selecting this + option will switch to Nightly. +

+); + const WasmAside: React.SFC = () => (

Note: WASM currently requires using the Nightly channel, selecting this diff --git a/ui/frontend/actions.ts b/ui/frontend/actions.ts index ebd95f3e8..0cbd73f3e 100644 --- a/ui/frontend/actions.ts +++ b/ui/frontend/actions.ts @@ -83,6 +83,9 @@ export enum ActionType { CompileLlvmIrRequest = 'COMPILE_LLVM_IR_REQUEST', CompileLlvmIrSucceeded = 'COMPILE_LLVM_IR_SUCCEEDED', CompileLlvmIrFailed = 'COMPILE_LLVM_IR_FAILED', + CompileHirRequest = 'COMPILE_HIR_REQUEST', + CompileHirSucceeded = 'COMPILE_HIR_SUCCEEDED', + CompileHirFailed = 'COMPILE_HIR_FAILED', CompileMirRequest = 'COMPILE_MIR_REQUEST', CompileMirSucceeded = 'COMPILE_MIR_SUCCEEDED', CompileMirFailed = 'COMPILE_MIR_FAILED', @@ -346,6 +349,27 @@ const performCompileToLLVMOnly = () => failure: receiveCompileLlvmIrFailure, }); +const requestCompileHir = () => + createAction(ActionType.CompileHirRequest); + +const receiveCompileHirSuccess = ({ code, stdout, stderr }) => + createAction(ActionType.CompileHirSucceeded, { code, stdout, stderr }); + +const receiveCompileHirFailure = ({ error }) => + createAction(ActionType.CompileHirFailed, { error }); + +const performCompileToHirOnly = () => + performCompileShow('hir', { + request: requestCompileHir, + success: receiveCompileHirSuccess, + failure: receiveCompileHirFailure, + }); + +const performCompileToNightlyHirOnly = (): ThunkAction => dispatch => { + dispatch(changeChannel(Channel.Nightly)); + dispatch(performCompileToHirOnly()); +}; + const requestCompileMir = () => createAction(ActionType.CompileMirRequest); @@ -390,6 +414,7 @@ const PRIMARY_ACTIONS: { [index in PrimaryAction]: () => ThunkAction } = { [PrimaryActionCore.Test]: performTestOnly, [PrimaryActionAuto.Auto]: performAutoOnly, [PrimaryActionCore.LlvmIr]: performCompileToLLVMOnly, + [PrimaryActionCore.Hir]: performCompileToHirOnly, [PrimaryActionCore.Mir]: performCompileToMirOnly, [PrimaryActionCore.Wasm]: performCompileToNightlyWasmOnly, }; @@ -417,6 +442,8 @@ export const performCompileToLLVM = performAndSwitchPrimaryAction(performCompileToLLVMOnly, PrimaryActionCore.LlvmIr); export const performCompileToMir = performAndSwitchPrimaryAction(performCompileToMirOnly, PrimaryActionCore.Mir); +export const performCompileToNightlyHir = + performAndSwitchPrimaryAction(performCompileToNightlyHirOnly, PrimaryActionCore.Hir); export const performCompileToNightlyWasm = performAndSwitchPrimaryAction(performCompileToNightlyWasmOnly, PrimaryActionCore.Wasm); diff --git a/ui/frontend/selectors/index.ts b/ui/frontend/selectors/index.ts index 70ae36f1a..890d1f149 100644 --- a/ui/frontend/selectors/index.ts +++ b/ui/frontend/selectors/index.ts @@ -71,6 +71,7 @@ const LABELS: { [index in PrimaryActionCore]: string } = { [PrimaryActionCore.Compile]: 'Build', [PrimaryActionCore.Execute]: 'Run', [PrimaryActionCore.LlvmIr]: 'Show LLVM IR', + [PrimaryActionCore.Hir]: 'Show HIR', [PrimaryActionCore.Mir]: 'Show MIR', [PrimaryActionCore.Test]: 'Test', [PrimaryActionCore.Wasm]: 'Show WASM', @@ -102,7 +103,7 @@ export const miriVersionDetailsText = createSelector([getMiri], versionDetails); const editionSelector = (state: State) => state.configuration.edition; -export const isWasmAvailable = (state: State) => ( +export const isNightlyChannel = (state: State) => ( state.configuration.channel === Channel.Nightly ); diff --git a/ui/frontend/types.ts b/ui/frontend/types.ts index 2f8e7cc68..159faf75d 100644 --- a/ui/frontend/types.ts +++ b/ui/frontend/types.ts @@ -74,6 +74,7 @@ export enum PrimaryActionCore { Compile = 'compile', Execute = 'execute', LlvmIr = 'llvm-ir', + Hir = 'hir', Mir = 'mir', Test = 'test', Wasm = 'wasm', diff --git a/ui/src/main.rs b/ui/src/main.rs index 004b30a75..2421bb8f2 100644 --- a/ui/src/main.rs +++ b/ui/src/main.rs @@ -934,6 +934,7 @@ fn parse_target(s: &str) -> Result { sandbox::ProcessAssembly::Filter), "llvm-ir" => sandbox::CompileTarget::LlvmIr, "mir" => sandbox::CompileTarget::Mir, + "hir" => sandbox::CompileTarget::Hir, "wasm" => sandbox::CompileTarget::Wasm, value => InvalidTarget { value }.fail()?, }) diff --git a/ui/src/sandbox.rs b/ui/src/sandbox.rs index 153ccf5f3..2b529d088 100644 --- a/ui/src/sandbox.rs +++ b/ui/src/sandbox.rs @@ -169,6 +169,8 @@ impl Sandbox { if process == ProcessAssembly::Filter { code = super::asm_cleanup::filter_asm(&code); } + } else if CompileTarget::Hir == req.target { + // TODO: Run rustfmt on the generated HIR. } Ok(CompileResponse { @@ -479,7 +481,13 @@ fn build_execution_command(target: Option, channel: Channel, mode } if let Some(target) = target { - cmd.extend(&["--", "-o", "/playground-result/compilation"]); + cmd.extend(&["--", "-o"]); + if target == Hir { + // -Zunpretty=hir only emits the HIR, not the binary itself + cmd.push("/playground-result/compilation.hir"); + } else { + cmd.push("/playground-result/compilation"); + } match target { Assembly(flavor, _, _) => { @@ -501,6 +509,7 @@ fn build_execution_command(target: Option, channel: Channel, mode }, LlvmIr => cmd.push("--emit=llvm-ir"), Mir => cmd.push("--emit=mir"), + Hir => cmd.push("-Zunpretty=hir"), Wasm => { /* handled by cargo-wasm wrapper */ }, } } @@ -612,6 +621,7 @@ pub enum CompileTarget { Assembly(AssemblyFlavor, DemangleAssembly, ProcessAssembly), LlvmIr, Mir, + Hir, Wasm, } @@ -621,6 +631,7 @@ impl CompileTarget { CompileTarget::Assembly(_, _, _) => "s", CompileTarget::LlvmIr => "ll", CompileTarget::Mir => "mir", + CompileTarget::Hir => "hir", CompileTarget::Wasm => "wat", }; OsStr::new(ext) @@ -635,6 +646,7 @@ impl fmt::Display for CompileTarget { Assembly(_, _, _) => "assembly".fmt(f), LlvmIr => "LLVM IR".fmt(f), Mir => "Rust MIR".fmt(f), + Hir => "Rust HIR".fmt(f), Wasm => "WebAssembly".fmt(f), } }