Skip to content
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
7 changes: 0 additions & 7 deletions demo/Cargo.lock

This file was deleted.

4 changes: 2 additions & 2 deletions demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion demo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
82 changes: 82 additions & 0 deletions demo/example/methods.rs
Original file line number Diff line number Diff line change
@@ -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>) {
self.by_ref("by_box");
Box::into_inner(self).by_value();
}

pub fn by_rc(self: Rc<Self>) {
self.by_ref("by_rc");
}

pub fn by_arc(self: Arc<Self>) {
self.by_ref("by_arc");
}

pub fn by_pin(self: Pin<&Self>) {
self.by_ref("by_pin");
}

pub fn explicit_type(self: Arc<Example>) {
self.by_ref("explicit");
}

pub fn with_lifetime<'a>(self: &'a Self) {
self.by_ref("lifetime");
}

pub fn nested<'a>(self: &mut &'a Arc<Rc<Box<Alias>>>) {
self.by_ref("nested");
}

pub fn via_projection(self: <Example as Trait>::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();
}
6 changes: 2 additions & 4 deletions demo/run_demo.sh
Original file line number Diff line number Diff line change
@@ -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
27 changes: 13 additions & 14 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,41 @@

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<String> = std::env::args().collect();
let result = run!(&rustc_args, start_demo);
match result {
Ok(_) | Err(CompilerError::Skipped | CompilerError::Interrupted(_)) => ExitCode::SUCCESS,
_ => ExitCode::FAILURE,
}
}

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(())
Expand Down