Add comments to explain the test case and the special treatment
This commit is contained in:
parent
ddbe69a5b2
commit
3b2642ffa7
2 changed files with 22 additions and 2 deletions
|
@ -506,6 +506,10 @@ impl<'tcx> Validator<'_, 'tcx> {
|
||||||
match *elem {
|
match *elem {
|
||||||
ProjectionElem::Deref => {
|
ProjectionElem::Deref => {
|
||||||
let mut not_promotable = true;
|
let mut not_promotable = true;
|
||||||
|
// This is a special treatment for cases like *&STATIC where STATIC is a
|
||||||
|
// global static variable.
|
||||||
|
// This pattern is generated only when global static variables are directly
|
||||||
|
// accessed and is qualified for promotion safely.
|
||||||
if let TempState::Defined { location, .. } = self.temps[local] {
|
if let TempState::Defined { location, .. } = self.temps[local] {
|
||||||
let def_stmt =
|
let def_stmt =
|
||||||
self.body[location.block].statements.get(location.statement_index);
|
self.body[location.block].statements.get(location.statement_index);
|
||||||
|
@ -517,7 +521,15 @@ impl<'tcx> Validator<'_, 'tcx> {
|
||||||
{
|
{
|
||||||
if let Some(did) = c.check_static_ptr(self.tcx) {
|
if let Some(did) = c.check_static_ptr(self.tcx) {
|
||||||
if let Some(hir::ConstContext::Static(..)) = self.const_kind {
|
if let Some(hir::ConstContext::Static(..)) = self.const_kind {
|
||||||
if !self.tcx.is_thread_local_static(did) {
|
// The `is_empty` predicate is introduced to exclude the case
|
||||||
|
// where the projection operations are [ .field, * ].
|
||||||
|
// The reason is because promotion will be illegal if field
|
||||||
|
// accesses preceed the dereferencing.
|
||||||
|
// Discussion can be found at
|
||||||
|
// https://github.com/rust-lang/rust/pull/74945#discussion_r463063247
|
||||||
|
// There may be opportunity for generalization, but this needs to be
|
||||||
|
// accounted for.
|
||||||
|
if proj_base.is_empty() && !self.tcx.is_thread_local_static(did) {
|
||||||
not_promotable = false;
|
not_promotable = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
// run-pass
|
// check-pass
|
||||||
|
|
||||||
|
// Use of global static variables in literal values should be allowed for
|
||||||
|
// promotion.
|
||||||
|
// This test is to demonstrate the issue raised in
|
||||||
|
// https://github.com/rust-lang/rust/issues/70584
|
||||||
|
|
||||||
|
// Literal values were previously promoted into local static values when
|
||||||
|
// other global static variables are used.
|
||||||
|
|
||||||
struct A<T: 'static>(&'static T);
|
struct A<T: 'static>(&'static T);
|
||||||
struct B<T: 'static + ?Sized> {
|
struct B<T: 'static + ?Sized> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue