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

Don't generate fake IDs in ReplaceBodyWithLoop #73532

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ pub fn run_compiler(
compiler.output_file().as_ref().map(|p| &**p),
);
}
trace!("finished pretty-printing");
return early_exit();
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub fn run_compiler_in_existing_thread_pool<R>(
}

pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
log::trace!("run_compiler");
let stderr = config.stderr.take();
util::spawn_thread_pool(
config.opts.edition,
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub fn configure_and_expand(
krate: ast::Crate,
crate_name: &str,
) -> Result<(ast::Crate, BoxedResolver)> {
log::trace!("configure_and_expand");
// Currently, we ignore the name resolution data structures for the purposes of dependency
// tracking. Instead we will run name resolution and include its output in the hash of each
// item, much like we do for macro expansion. In other words, the hash reflects not just
Expand Down Expand Up @@ -230,6 +231,7 @@ fn configure_and_expand_inner<'a>(
resolver_arenas: &'a ResolverArenas<'a>,
metadata_loader: &'a MetadataLoaderDyn,
) -> Result<(ast::Crate, Resolver<'a>)> {
log::trace!("configure_and_expand_inner");
pre_expansion_lint(sess, lint_store, &krate);

let mut resolver = Resolver::new(sess, &krate, crate_name, metadata_loader, &resolver_arenas);
Expand Down Expand Up @@ -357,6 +359,7 @@ fn configure_and_expand_inner<'a>(
should_loop |= true;
}
if should_loop {
log::debug!("replacing bodies with loop {{}}");
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<'tcx> Queries<'tcx> {
pub fn expansion(
&self,
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
log::trace!("expansion");
self.expansion.compute(|| {
let crate_name = self.crate_name()?.peek().clone();
let (krate, lint_store) = self.register_plugins()?.take();
Expand Down
75 changes: 28 additions & 47 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::info;
use rustc_ast::ast::{AttrVec, BlockCheckMode};
use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
use rustc_ast::mut_visit::{MutVisitor, *};
use rustc_ast::ptr::P;
use rustc_ast::util::lev_distance::find_best_match_for_name;
use rustc_ast::{self, ast};
Expand Down Expand Up @@ -29,7 +29,6 @@ use smallvec::SmallVec;
use std::env;
use std::io::{self, Write};
use std::mem;
use std::ops::DerefMut;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, Once};
#[cfg(not(parallel_compiler))]
Expand Down Expand Up @@ -593,21 +592,21 @@ pub fn build_output_filenames(
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
pub struct ReplaceBodyWithLoop<'a, 'b> {
within_static_or_const: bool,
nested_blocks: Option<Vec<ast::Block>>,
nested_items: Option<Vec<ast::Stmt>>,
resolver: &'a mut Resolver<'b>,
}

impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
pub fn new(resolver: &'a mut Resolver<'b>) -> ReplaceBodyWithLoop<'a, 'b> {
ReplaceBodyWithLoop { within_static_or_const: false, nested_blocks: None, resolver }
ReplaceBodyWithLoop { within_static_or_const: false, nested_items: None, resolver }
}

fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, is_const: bool, action: F) -> R {
let old_const = mem::replace(&mut self.within_static_or_const, is_const);
let old_blocks = self.nested_blocks.take();
let old_blocks = self.nested_items.take();
let ret = action(self);
self.within_static_or_const = old_const;
self.nested_blocks = old_blocks;
self.nested_items = old_blocks;
ret
}

Expand Down Expand Up @@ -694,6 +693,8 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
}

fn visit_block(&mut self, b: &mut P<ast::Block>) {
// WARNING: this generates a dummy span and so should not be made the parent of any stmt that is not a dummy.
// See https://github.com/rust-lang/rust/issues/71104.
fn stmt_to_block(
rules: ast::BlockCheckMode,
s: Option<ast::Stmt>,
Expand All @@ -707,22 +708,6 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
}
}

fn block_to_stmt(b: ast::Block, resolver: &mut Resolver<'_>) -> ast::Stmt {
let expr = P(ast::Expr {
id: resolver.next_node_id(),
kind: ast::ExprKind::Block(P(b), None),
span: rustc_span::DUMMY_SP,
attrs: AttrVec::new(),
tokens: None,
});

ast::Stmt {
id: resolver.next_node_id(),
kind: ast::StmtKind::Expr(expr),
span: rustc_span::DUMMY_SP,
}
}

let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.resolver);
let loop_expr = P(ast::Expr {
kind: ast::ExprKind::Loop(P(empty_block), None),
Expand All @@ -739,37 +724,33 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
};

if self.within_static_or_const {
noop_visit_block(b, self)
} else {
visit_clobber(b.deref_mut(), |b| {
let mut stmts = vec![];
for s in b.stmts {
let old_blocks = self.nested_blocks.replace(vec![]);
return noop_visit_block(b, self);
}

stmts.extend(self.flat_map_stmt(s).into_iter().filter(|s| s.is_item()));
let mut items = vec![];
for s in b.stmts.drain(..) {
let old_items = self.nested_items.replace(vec![]);

// we put a Some in there earlier with that replace(), so this is valid
let new_blocks = self.nested_blocks.take().unwrap();
self.nested_blocks = old_blocks;
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
}
items.extend(self.flat_map_stmt(s).into_iter().filter(|s| s.is_item()));

let mut new_block = ast::Block { stmts, ..b };
// we put a Some in there earlier with that replace(), so this is valid
let nested_items = self.nested_items.take().unwrap();
self.nested_items = old_items;
items.extend(nested_items);
}

if let Some(old_blocks) = self.nested_blocks.as_mut() {
//push our fresh block onto the cache and yield an empty block with `loop {}`
if !new_block.stmts.is_empty() {
old_blocks.push(new_block);
}
if let Some(nested_items) = self.nested_items.as_mut() {
// add our items to the existing statements and yield an empty block with `loop {}`
if !items.is_empty() {
nested_items.extend(items);
}

stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
} else {
//push `loop {}` onto the end of our fresh block and yield that
new_block.stmts.push(loop_stmt);
b.stmts = vec![loop_stmt];
} else {
// push `loop {}` onto the end of our fresh block and yield that
items.push(loop_stmt);

new_block
}
})
b.stmts = items;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/librustc_session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ fn parse_pretty(
}
}
};
log::debug!("got unpretty option: {:?}", first);
first
}
}
Expand Down Expand Up @@ -1954,9 +1955,11 @@ impl PpMode {
use PpMode::*;
use PpSourceMode::*;
match *self {
PpmSource(PpmNormal | PpmEveryBodyLoops | PpmIdentified) => false,
PpmSource(PpmNormal | PpmIdentified) => false,

PpmSource(PpmExpanded | PpmExpandedIdentified | PpmExpandedHygiene)
PpmSource(
PpmExpanded | PpmEveryBodyLoops | PpmExpandedIdentified | PpmExpandedHygiene,
)
| PpmHir(_)
| PpmHirTree(_)
| PpmMir
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc/macro-in-async-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Regression issue for rustdoc ICE encountered in PR #72088.
// edition:2018
#![feature(decl_macro)]

fn main() {
async {
macro m() {}
};
}
7 changes: 7 additions & 0 deletions src/test/rustdoc/macro-in-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ fn main() {
|| {
macro m() {}
};

let _ = || {
macro n() {}
};

let cond = true;
let _ = || if cond { macro n() {} } else { panic!() };
}