# Syntax extension for static stack analysis

**URL:** <https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411>\
**Category:** ideas (deprecated)\
**Created:** [August 19, 2014, 8:09pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411 "2014-08-19T20:09:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bharrisau](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bharrisau/32/205_2.png) [@bharrisau](https://internals.rust-lang.org/u/bharrisau)\
**Post date:** [August 19, 2014, 8:09pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/1 "2014-08-19T20:09:05Z")

</div>

Stay calm - this is no more than an idea in progress.

I’m curious if anyone else is looking into stack safety through compile time analysis - a subset of Rust without recursion or calloca. Based on my initial investigation, there would need to be some small changes to the LLVM shim to enable calling `addPassesToEmitFile()` with a `StopAfter` as the prologue pass.

I could see a lint copying the AST, doing a dummy codegen pass and extracting the stack sizes from the resulting LLVM context.

Is anyone doing work in this area already?

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [August 20, 2014, 11:55am UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/2 "2014-08-20T11:55:47Z")

</div>

I think you also need to disallow virtual function calls. As far as I know, Rust’s IO uses virtual function calls, so you woudln’t even have this…

---

<div class="post-metadata">

**Author:** ![arcto](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arcto/32/45_2.png) [@arcto](https://internals.rust-lang.org/u/arcto)\
**Post date:** [August 20, 2014, 12:41pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/3 "2014-08-20T12:41:29Z")

</div>

Are virtual function calls not just conditional calls, i.e.

```
match type_id {
   A => a_foo(),
   B => b_foo(),
   _ => base_foo()
}

```

and thus, the maximum stack usage can be inferred?

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [August 20, 2014, 4:32pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/4 "2014-08-20T16:32:19Z")

</div>

Virtual function calls need a struct (usually implicit via a trait):

```
struct Virtual {
    func: fn (int) -> uint,
}

```

Then a virtual function call looks like this:

```
fn do_stuff(v: Virtual) -> uint {
    v.func(10)
}

```

`v.func` could be any function with that signature…

---

<div class="post-metadata">

**Author:** ![bharrisau](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bharrisau/32/205_2.png) [@bharrisau](https://internals.rust-lang.org/u/bharrisau)\
**Post date:** [August 21, 2014, 5:12am UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/5 "2014-08-21T05:12:37Z")

</div>

Are you talking about dynamic dispatch for `Box<Virtual>`? In that case we could just get the maximum stack of any impl?

---

<div class="post-metadata">

**Author:** ![bharrisau](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bharrisau/32/205_2.png) [@bharrisau](https://internals.rust-lang.org/u/bharrisau)\
**Post date:** [August 21, 2014, 2:59pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/6 "2014-08-21T14:59:53Z")

</div>

Note: Don’t post after long plane trips.

Totally mis-read your post sorry. Yep, there are some things you can’t handle - but we can handle the majority of cases and stick the rest in `unsafe` for the time being.

But it doesn’t look like this is on many people’s radar.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [August 21, 2014, 10:45pm UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/7 "2014-08-21T22:45:49Z")

</div>

> [@bharrisau](#):
>
> Are you talking about dynamic dispatch for `Box<Virtual>`? In that case we could just get the maximum stack of any impl?

I was actually talking about this, yes. The problem is that you need to know the whole program for this analysis.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:22am UTC](https://internals.rust-lang.org/t/syntax-extension-for-static-stack-analysis/411/8 "2019-03-25T08:22:48Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
