Skip to content
Draft
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
30 changes: 3 additions & 27 deletions compiler/rustc_mir_transform/src/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! This pass assumes that every use is dominated by an initialization and can
//! otherwise silence errors, if move analysis runs after promotion on broken
//! MIR.
#![allow(unused)]

use std::assert_matches::assert_matches;
use std::cell::Cell;
Expand Down Expand Up @@ -662,33 +663,8 @@ impl<'tcx> Validator<'_, 'tcx> {
}
}

// Ideally, we'd stop here and reject the rest.
// But for backward compatibility, we have to accept some promotion in const/static
// initializers. Inline consts are explicitly excluded, they are more recent so we have no
// backwards compatibility reason to allow more promotion inside of them.
let promote_all_fn = matches!(
self.const_kind,
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
);
if !promote_all_fn {
return Err(Unpromotable);
}
// Make sure the callee is a `const fn`.
let is_const_fn = match *fn_ty.kind() {
ty::FnDef(def_id, _) => self.tcx.is_const_fn(def_id),
_ => false,
};
if !is_const_fn {
return Err(Unpromotable);
}
// The problem is, this may promote calls to functions that panic.
// We don't want to introduce compilation errors if there's a panic in a call in dead code.
// So we ensure that this is not dead code.
if !self.is_promotion_safe_block(block) {
return Err(Unpromotable);
}
// This passed all checks, so let's accept.
Ok(())
// Ideally, we'd stop here and reject the rest. So let's do that.
return Err(Unpromotable);
}
}

Expand Down
Loading