Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -Zunpretty=hir #683

Merged
merged 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/spec/features/compilation_targets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
end
end

scenario "compiling to HIR" do
editor.set <<~EOF
fn demo() -> impl std::fmt::Display { 42 }
EOF

in_build_menu { click_on("HIR") }

within('.output-result') do
expect(page).to have_content 'fn demo() -> /*impl Trait*/ { 42 }'
end
end

scenario "compiling to WebAssembly" do
in_build_menu { click_on("WASM") }

Expand Down
15 changes: 14 additions & 1 deletion ui/frontend/BuildMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const useDispatchAndClose = (action: () => void, close: () => void) => {
}

const BuildMenu: React.SFC<BuildMenuProps> = props => {
const isHirAvailable = useSelector(selectors.isHirAvailable);
const isWasmAvailable = useSelector(selectors.isWasmAvailable);

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);
Expand All @@ -55,7 +57,11 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
Build and show the resulting LLVM IR, LLVM’s intermediate representation.
</ButtonMenuItem>
<ButtonMenuItem name="MIR" onClick={compileToMir}>
Build and show the resulting MIR, Rust’s intermediate representation.
Build and show the resulting MIR, Rust’s control-flow-based intermediate representation.
</ButtonMenuItem>
<ButtonMenuItem name="HIR" onClick={compileToHir}>
Build and show the resulting HIR, Rust’s syntax-based intermediate representation.
{!isHirAvailable && <HirAside />}
</ButtonMenuItem>
<ButtonMenuItem name="WASM" onClick={compileToWasm}>
Build a WebAssembly module for web browsers, in the .WAT textual representation.
Expand All @@ -65,6 +71,13 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
);
};

const HirAside: React.SFC = () => (
<p className="build-menu__aside">
Note: HIR currently requires using the Nightly channel, selecting this
option will switch to Nightly.
</p>
);

const WasmAside: React.SFC = () => (
<p className="build-menu__aside">
Note: WASM currently requires using the Nightly channel, selecting this
Expand Down
8 changes: 7 additions & 1 deletion ui/frontend/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface PaneWithCodeProps extends SimplePaneProps {

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

const dispatch = useDispatch();
Expand All @@ -59,6 +59,7 @@ const Output: React.SFC = () => {
const focusAssembly = useCallback(() => dispatch(actions.changeFocus(Focus.Asm)), [dispatch]);
const focusLlvmIr = useCallback(() => dispatch(actions.changeFocus(Focus.LlvmIr)), [dispatch]);
const focusMir = useCallback(() => dispatch(actions.changeFocus(Focus.Mir)), [dispatch]);
const focusHir = useCallback(() => dispatch(actions.changeFocus(Focus.Hir)), [dispatch]);
const focusWasm = useCallback(() => dispatch(actions.changeFocus(Focus.Wasm)), [dispatch]);
const focusGist = useCallback(() => dispatch(actions.changeFocus(Focus.Gist)), [dispatch]);

Expand All @@ -84,6 +85,7 @@ const Output: React.SFC = () => {
{focus === Focus.Asm && <PaneWithCode {...assembly} kind="asm" />}
{focus === Focus.LlvmIr && <PaneWithCode {...llvmIr} kind="llvm-ir" />}
{focus === Focus.Mir && <PaneWithMir {...mir} kind="mir" />}
{focus === Focus.Hir && <PaneWithMir {...hir} kind="hir" />}
{focus === Focus.Wasm && <PaneWithCode {...wasm} kind="wasm" />}
{focus === Focus.Gist && <Gist />}
</div>
Expand Down Expand Up @@ -125,6 +127,10 @@ const Output: React.SFC = () => {
label="MIR"
onClick={focusMir}
tabProps={mir} />
<Tab kind={Focus.Hir} focus={focus}
label="HIR"
onClick={focusHir}
tabProps={hir} />
<Tab kind={Focus.Wasm} focus={focus}
label="WASM"
onClick={focusWasm}
Expand Down
30 changes: 30 additions & 0 deletions ui/frontend/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -790,6 +817,9 @@ export type Action =
| ReturnType<typeof requestCompileMir>
| ReturnType<typeof receiveCompileMirSuccess>
| ReturnType<typeof receiveCompileMirFailure>
| ReturnType<typeof requestCompileHir>
| ReturnType<typeof receiveCompileHirSuccess>
| ReturnType<typeof receiveCompileHirFailure>
| ReturnType<typeof requestCompileWasm>
| ReturnType<typeof receiveCompileWasmSuccess>
| ReturnType<typeof receiveCompileWasmFailure>
Expand Down
33 changes: 33 additions & 0 deletions ui/frontend/reducers/output/hir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Action, ActionType } from '../../actions';
import { finish, start } from './sharedStateManagement';

const DEFAULT: State = {
requestsInProgress: 0,
code: null,
stdout: null,
stderr: null,
error: null,
};

interface State {
requestsInProgress: number;
code?: string;
stdout?: string;
stderr?: string;
error?: string;
}

export default function hir(state = DEFAULT, action: Action) {
switch (action.type) {
case ActionType.CompileHirRequest:
return start(DEFAULT, state);
case ActionType.CompileHirSucceeded: {
const { code = '', stdout = '', stderr = '' } = action;
return finish(state, { code, stdout, stderr });
}
case ActionType.CompileHirFailed:
return finish(state, { error: action.error });
default:
return state;
}
}
4 changes: 3 additions & 1 deletion ui/frontend/reducers/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import clippy from './clippy';
import execute from './execute';
import format from './format';
import gist from './gist';
import hir from './hir';
import llvmIr from './llvmIr';
import macroExpansion from './macroExpansion';
import meta from './meta';
import mir from './mir';
import miri from './miri';
import macroExpansion from './macroExpansion';
import wasm from './wasm';

const output = combineReducers({
Expand All @@ -21,6 +22,7 @@ const output = combineReducers({
assembly,
llvmIr,
mir,
hir,
wasm,
execute,
gist,
Expand Down
3 changes: 3 additions & 0 deletions ui/frontend/reducers/output/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default function meta(state = DEFAULT, action: Action) {
case ActionType.CompileMirRequest:
return { ...state, focus: Focus.Mir };

case ActionType.CompileHirRequest:
return { ...state, focus: Focus.Hir };

case ActionType.CompileWasmRequest:
return { ...state, focus: Focus.Wasm };

Expand Down
6 changes: 5 additions & 1 deletion ui/frontend/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -102,9 +103,11 @@ 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
);
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
export const isWasmAvailable = isNightlyChannel;
export const isHirAvailable = isNightlyChannel;

export const getModeLabel = (state: State) => {
const { configuration: { mode } } = state;
Expand Down Expand Up @@ -142,6 +145,7 @@ const getOutputs = (state: State) => [
state.output.gist,
state.output.llvmIr,
state.output.mir,
state.output.hir,
state.output.miri,
state.output.macroExpansion,
state.output.wasm,
Expand Down
2 changes: 2 additions & 0 deletions ui/frontend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export enum PrimaryActionCore {
Compile = 'compile',
Execute = 'execute',
LlvmIr = 'llvm-ir',
Hir = 'hir',
Mir = 'mir',
Test = 'test',
Wasm = 'wasm',
Expand Down Expand Up @@ -108,6 +109,7 @@ export enum Focus {
MacroExpansion = 'macro-expansion',
LlvmIr = 'llvm-ir',
Mir = 'mir',
Hir = 'hir',
Wasm = 'wasm',
Asm = 'asm',
Execute = 'execute',
Expand Down
1 change: 1 addition & 0 deletions ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ fn parse_target(s: &str) -> Result<sandbox::CompileTarget> {
sandbox::ProcessAssembly::Filter),
"llvm-ir" => sandbox::CompileTarget::LlvmIr,
"mir" => sandbox::CompileTarget::Mir,
"hir" => sandbox::CompileTarget::Hir,
"wasm" => sandbox::CompileTarget::Wasm,
value => InvalidTarget { value }.fail()?,
})
Expand Down
14 changes: 13 additions & 1 deletion ui/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -479,7 +481,13 @@ fn build_execution_command(target: Option<CompileTarget>, 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, _, _) => {
Expand All @@ -501,6 +509,7 @@ fn build_execution_command(target: Option<CompileTarget>, 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 */ },
}
}
Expand Down Expand Up @@ -612,6 +621,7 @@ pub enum CompileTarget {
Assembly(AssemblyFlavor, DemangleAssembly, ProcessAssembly),
LlvmIr,
Mir,
Hir,
Wasm,
}

Expand All @@ -621,6 +631,7 @@ impl CompileTarget {
CompileTarget::Assembly(_, _, _) => "s",
CompileTarget::LlvmIr => "ll",
CompileTarget::Mir => "mir",
CompileTarget::Hir => "hir",
CompileTarget::Wasm => "wat",
};
OsStr::new(ext)
Expand All @@ -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),
}
}
Expand Down