From a15e0cbe29f9e3afaabfcf6a07ff37181660a46d Mon Sep 17 00:00:00 2001 From: Makai Date: Thu, 21 Aug 2025 02:04:42 +0800 Subject: [PATCH] fix demo --- .github/workflows/demo.yml | 4 +- demo/Cargo.lock | 7 ---- demo/Cargo.toml | 4 +- demo/build.rs | 2 +- demo/example/methods.rs | 82 ++++++++++++++++++++++++++++++++++++++ demo/run_demo.sh | 6 +-- demo/src/main.rs | 27 ++++++------- 7 files changed, 102 insertions(+), 30 deletions(-) delete mode 100644 demo/Cargo.lock create mode 100644 demo/example/methods.rs diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 86249c5..3298129 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -1,4 +1,4 @@ -# Run a job to ensure formatting is OK +# Run a job to ensure that we can run the demo successfully. name: Run demo on: pull_request: @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run Demo run: ./demo/run_demo.sh diff --git a/demo/Cargo.lock b/demo/Cargo.lock deleted file mode 100644 index 05729f1..0000000 --- a/demo/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "smir-demo" -version = "0.0.0" diff --git a/demo/Cargo.toml b/demo/Cargo.toml index 7ab12d7..c9320e8 100644 --- a/demo/Cargo.toml +++ b/demo/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "smir-demo" -description = "A little demo tool on how to use StableMIR to analyze crates" +name = "rpub-demo" +description = "A little demo tool on how to use rustc_public to analyze crates" version = "0.0.0" edition = "2021" diff --git a/demo/build.rs b/demo/build.rs index e3fdffb..ecbfc70 100644 --- a/demo/build.rs +++ b/demo/build.rs @@ -9,7 +9,7 @@ pub fn main() { .iter() .collect(); println!( - "cargo:rustc-link-arg-bin=smir-demo=-Wl,-rpath,{}", + "cargo:rustc-link-arg-bin=rpub-demo=-Wl,-rpath,{}", rustc_lib.display() ); } diff --git a/demo/example/methods.rs b/demo/example/methods.rs new file mode 100644 index 0000000..17ba9ac --- /dev/null +++ b/demo/example/methods.rs @@ -0,0 +1,82 @@ +#![feature(box_into_inner)] + +use std::pin::Pin; +use std::rc::Rc; +use std::sync::Arc; + +#[derive(Default, Debug)] +struct Example { + inner: String, +} + +type Alias = Example; +trait Trait { + type Output; +} +impl Trait for Example { + type Output = Example; +} + +#[allow(unused)] +impl Example { + pub fn by_value(self: Self) { + self.by_ref("by_val"); + } + + pub fn by_ref(self: &Self, source: &str) { + println!("{source}: {}", self.inner); + } + + pub fn by_ref_mut(self: &mut Self) { + self.inner = "by_ref_mut".to_string(); + self.by_ref("mut"); + } + + pub fn by_box(self: Box) { + self.by_ref("by_box"); + Box::into_inner(self).by_value(); + } + + pub fn by_rc(self: Rc) { + self.by_ref("by_rc"); + } + + pub fn by_arc(self: Arc) { + self.by_ref("by_arc"); + } + + pub fn by_pin(self: Pin<&Self>) { + self.by_ref("by_pin"); + } + + pub fn explicit_type(self: Arc) { + self.by_ref("explicit"); + } + + pub fn with_lifetime<'a>(self: &'a Self) { + self.by_ref("lifetime"); + } + + pub fn nested<'a>(self: &mut &'a Arc>>) { + self.by_ref("nested"); + } + + pub fn via_projection(self: ::Output) { + self.by_ref("via_projection"); + } + + pub fn from(name: String) -> Self { + Example { inner: name } + } +} + +fn main() { + let example = Example::from("Hello".to_string()); + example.by_value(); + + let boxed = Box::new(Example::default()); + boxed.by_box(); + + Example::default().by_ref_mut(); + Example::default().with_lifetime(); +} diff --git a/demo/run_demo.sh b/demo/run_demo.sh index b761a22..84ff3af 100755 --- a/demo/run_demo.sh +++ b/demo/run_demo.sh @@ -1,10 +1,8 @@ #!/usr/bin/env bash -# Builds and run the demo driver against itself. -# I.e.: This bootstrap itself. +# Builds and run the demo driver against an example. REPO_DIR=$(git rev-parse --show-toplevel) DEMO_DIR="${REPO_DIR}/demo" cd "${DEMO_DIR}" -cargo run -- src/main.rs --crate-name demo --edition 2021 -C panic=abort - +cargo run -- example/methods.rs --crate-name exp --edition 2021 -C panic=abort diff --git a/demo/src/main.rs b/demo/src/main.rs index a1d889a..4ba9d58 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -5,26 +5,25 @@ extern crate rustc_driver; extern crate rustc_interface; -#[macro_use] -extern crate rustc_smir; -extern crate stable_mir; +extern crate rustc_middle; +extern crate rustc_public; use std::collections::HashSet; use std::io::stdout; -use rustc_smir::{run, rustc_internal}; -use stable_mir::{CompilerError, CrateDef}; +use rustc_public::run; +use rustc_public::{CompilerError, CrateDef}; use std::ops::ControlFlow; use std::process::ExitCode; -use stable_mir::mir::{LocalDecl, MirVisitor, Terminator, TerminatorKind}; -use stable_mir::mir::mono::Instance; -use stable_mir::mir::visit::Location; -use stable_mir::ty::{RigidTy, Ty, TyKind}; +use rustc_public::mir::{LocalDecl, MirVisitor, Terminator, TerminatorKind}; +use rustc_public::mir::mono::Instance; +use rustc_public::mir::visit::Location; +use rustc_public::ty::{RigidTy, Ty, TyKind}; /// This is a wrapper that can be used to replace rustc. fn main() -> ExitCode { - let rustc_args = std::env::args().into_iter().collect(); - let result = run!(rustc_args, start_demo); + let rustc_args: Vec = std::env::args().collect(); + let result = run!(&rustc_args, start_demo); match result { Ok(_) | Err(CompilerError::Skipped | CompilerError::Interrupted(_)) => ExitCode::SUCCESS, _ => ExitCode::FAILURE, @@ -32,15 +31,15 @@ fn main() -> ExitCode { } fn start_demo() -> ControlFlow<()> { - let crate_name = stable_mir::local_crate().name; + let crate_name = rustc_public::local_crate().name; eprintln!("--- Analyzing crate: {crate_name}"); - let crate_items = stable_mir::all_local_items(); + let crate_items = rustc_public::all_local_items(); for item in crate_items { eprintln!(" - {} @{:?}", item.name(), item.span()) } - let entry_fn = stable_mir::entry_fn().unwrap(); + let entry_fn = rustc_public::entry_fn().unwrap(); let entry_instance = Instance::try_from(entry_fn).unwrap(); analyze_instance(entry_instance); ControlFlow::Break(())