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

Try to find cyclic data dependencies during const-checking #71526

Closed
Closed
Changes from 1 commit
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
27 changes: 22 additions & 5 deletions src/librustc_mir/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,31 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
);
}

fn visit_operand(&mut self, op: &Operand<'tcx>, location: Location) {
self.super_operand(op, location);
if let Operand::Constant(c) = op {
if let Some(def_id) = c.check_static_ptr(self.tcx) {
self.check_static(def_id, self.span);
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
self.super_constant(constant, location);

if let ty::ConstKind::Unevaluated(def_id, _, promoted) = constant.literal.val {
assert!(promoted.is_none(), "Const-checking should run before promotion");

// If a cyclic data dependency exists within a const initializer, try to find
// it during const-checking. This is important because MIR optimizations could
// eliminate a cycle before const-eval runs. See #71078 for an example of this.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#71078 isn't actually an example of this, as Ralf notes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? It isn't? Or are you talking about a comment by Ralf from elsewhere but this PR?

//
// FIXME: This means we don't look for cycles involving associated constants, but we
// should handle fully monomorphized ones here at least.
if self.tcx.trait_of_item(def_id).is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can invoke Instance::resolve here, which will either bail out or give you the correct assoc constant if it can be resolved with the information at hand.

let _ = self.tcx.at(self.span).mir_const_qualif(def_id);
}
}

if let Some(def_id) = constant.check_static_ptr(self.tcx) {
self.check_static(def_id, self.span);

// NOTE: Because we are allowed to refer to the address of a static within its
// initializer, we don't try to trigger cycle errors every time we see a static.
}
}

fn visit_projection_elem(
&mut self,
place_local: Local,
Expand Down