## Stream: t-compiler

### Topic: AST simplification pass

marmeladema (May 09 2020 at 13:54, on Zulip):

Hello! Is there any simplification pass done on the AST before lowering to HIR? I am trying to debug something and i don't understand most of it but would it be possible that an Expr of kind: Closure be transformed in its inner block? Or if you look at differently, that the Closure level is removed and its inner block left instead in the tree?

Vadim Petrochenkov (May 10 2020 at 01:00, on Zulip):

No simplification.

Vadim Petrochenkov (May 10 2020 at 01:01, on Zulip):

Besides regular macro expansion and a couple of extra expansion passes like test harness generation, all AST transformations and simplifications are happening during lowering to HIR.

Vadim Petrochenkov (May 10 2020 at 01:03, on Zulip):

However, some side tables later used from HIR are built as if AST were modified, e.g. DefId hierarchies (src\librustc_resolve\def_collector.rs).

marmeladema (May 10 2020 at 10:37, on Zulip):

Ok. So its during def_collector that i can see the Expr with kind: Closure and then later during during lowering i can only see the block of the cloure

marmeladema (May 10 2020 at 10:39, on Zulip):

So during def_collector i can see: main function -> block -> closure expr -> block -> macrodef
But during lowering i can see: main function -> block -> block -> macrodef

marmeladema (May 10 2020 at 10:39, on Zulip):

Because the closure expr is gone, its not lowered properly and the macrodef (which is properly lowered) has a parent def id that does not have an associated hir id

marmeladema (May 10 2020 at 10:41, on Zulip):

And i printed the AST at the beginning of the lower_crate function and the closure expr is already gone at this point so something must happen before

Vadim Petrochenkov (May 10 2020 at 11:41, on Zulip):

Hmm, is this happening in rustdoc by chance?

Vadim Petrochenkov (May 10 2020 at 11:42, on Zulip):

Because rustdoc does have a pass removing parts of AST unnecessary for documentation purposes.

marmeladema (May 10 2020 at 12:47, on Zulip):

Oh yes its during a rustdoc test

marmeladema (May 10 2020 at 12:47, on Zulip):

@Vadim Petrochenkov src/test/rustdoc/macro-in-closure.rs

Vadim Petrochenkov (May 10 2020 at 12:52, on Zulip):

The relevant pass is ReplaceBodyWithLoop.

Vadim Petrochenkov (May 10 2020 at 12:53, on Zulip):

AFAIK, it was modified in the past to not strip "things with items inside them", perhaps this is a missing case.

marmeladema (May 10 2020 at 12:53, on Zulip):

thank you very much for the help, i'll look for that and try to understand what is happening!

marmeladema (May 10 2020 at 13:22, on Zulip):

Something definitely happens in ReplaceBodyWithLoop::visit_block

marmeladema (May 10 2020 at 22:08, on Zulip):

So i managed to do a PR that fixes the issue: https://github.com/rust-lang/rust/pull/72088

marmeladema (May 12 2020 at 08:19, on Zulip):

This is kind of a nightmare because fixing in one place, make other places fail :(

Last update: May 29 2020 at 16:40UTC