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

[beta] Backport #67134 #67297

Closed
wants to merge 2 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
5 changes: 4 additions & 1 deletion src/librustc_codegen_ssa/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
instance,
promoted: None,
};
self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid))
self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid)).map_err(|err| {
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
err
})
},
_ => Ok(self.monomorphize(&constant.literal)),
}
Expand Down
11 changes: 8 additions & 3 deletions src/librustc_mir/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
let stmt =
&self.body.basic_blocks()[location.block].statements[location.statement_index];
if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(c)))) = &stmt.kind {
if p.as_local().is_some() {
trace!("skipping store of const value {:?} to {:?}", c, local);
return;
match c.literal.val {
// Keep assignments from unevaluated constants around, since the evaluation
// may report errors, even if the use of the constant is dead code.
interpret::ConstValue::Unevaluated(..) => {}
_ => if p.as_local().is_some() {
trace!("skipping store of const value {:?} to {:?}", c, p);
return;
},
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/consts/assoc_const_generic_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![warn(const_err)]

trait ZeroSized: Sized {
const I_AM_ZERO_SIZED: ();
fn requires_zero_size(self);
}

impl<T: Sized> ZeroSized for T {
const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()]; //~ WARN any use of this value
fn requires_zero_size(self) {
let () = Self::I_AM_ZERO_SIZED; //~ ERROR erroneous constant encountered
println!("requires_zero_size called");
}
}

fn main() {
().requires_zero_size();
42_u32.requires_zero_size();
}
22 changes: 22 additions & 0 deletions src/test/ui/consts/assoc_const_generic_impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
warning: any use of this value will cause an error
--> $DIR/assoc_const_generic_impl.rs:9:34
|
LL | const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()];
| -----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| index out of bounds: the len is 1 but the index is 4
|
note: lint level defined here
--> $DIR/assoc_const_generic_impl.rs:1:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^

error: erroneous constant encountered
--> $DIR/assoc_const_generic_impl.rs:11:18
|
LL | let () = Self::I_AM_ZERO_SIZED;
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error