Skip to content

Commit 7b7dc6e

Browse files
committed
Auto merge of #69146 - matthewjasper:literal-qualif, r=<try>
Always const qualify literals by type r? @eddyb
2 parents dbef353 + f2980e7 commit 7b7dc6e

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

src/librustc/ty/util.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl<'tcx> ty::TyS<'tcx> {
697697
/// strange rules like `<T as Foo<'static>>::Bar: Sized` that
698698
/// actually carry lifetime requirements.
699699
pub fn is_sized(&'tcx self, tcx_at: TyCtxtAt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
700-
tcx_at.is_sized_raw(param_env.and(self))
700+
self.is_trivially_sized(tcx_at.tcx) || tcx_at.is_sized_raw(param_env.and(self))
701701
}
702702

703703
/// Checks whether values of this type `T` implement the `Freeze`
@@ -713,7 +713,43 @@ impl<'tcx> ty::TyS<'tcx> {
713713
param_env: ty::ParamEnv<'tcx>,
714714
span: Span,
715715
) -> bool {
716-
tcx.at(span).is_freeze_raw(param_env.and(self))
716+
self.is_trivially_freeze() || tcx.at(span).is_freeze_raw(param_env.and(self))
717+
}
718+
719+
/// Fast path helper for testing if a type is `Freeze`.
720+
///
721+
/// Returning true means the type is known to be `Freeze`. Returning
722+
/// `false` means nothing -- could be `Freeze`, might not be.
723+
fn is_trivially_freeze(&self) -> bool {
724+
match self.kind {
725+
ty::Int(_)
726+
| ty::Uint(_)
727+
| ty::Float(_)
728+
| ty::Bool
729+
| ty::Char
730+
| ty::Str
731+
| ty::Never
732+
| ty::Ref(..)
733+
| ty::RawPtr(_)
734+
| ty::FnDef(..)
735+
| ty::Error
736+
| ty::FnPtr(_) => true,
737+
ty::Tuple(_) => self.tuple_fields().all(Self::is_trivially_freeze),
738+
ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_freeze(),
739+
ty::Adt(..)
740+
| ty::Bound(..)
741+
| ty::Closure(..)
742+
| ty::Dynamic(..)
743+
| ty::Foreign(_)
744+
| ty::Generator(..)
745+
| ty::GeneratorWitness(_)
746+
| ty::Infer(_)
747+
| ty::Opaque(..)
748+
| ty::Param(_)
749+
| ty::Placeholder(_)
750+
| ty::Projection(_)
751+
| ty::UnnormalizedProjection(_) => false,
752+
}
717753
}
718754

719755
/// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely

src/librustc_mir/transform/check_consts/qualifs.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,23 @@ pub trait Qualif {
9494
}
9595

9696
Operand::Constant(ref constant) => {
97-
if constant.check_static_ptr(cx.tcx).is_some() {
98-
// `mir_const_qualif` does return the qualifs in the final value of a `static`,
99-
// so we could use value-based qualification here, but we shouldn't do this
100-
// without a good reason.
101-
//
102-
// Note: this uses `constant.literal.ty` which is a reference or pointer to the
103-
// type of the actual `static` item.
104-
Self::in_any_value_of_ty(cx, constant.literal.ty)
105-
} else if let ty::ConstKind::Unevaluated(def_id, _, promoted) = constant.literal.val
106-
{
97+
// Check the qualifs of the value of `const` items.
98+
if let ty::ConstKind::Unevaluated(def_id, _, promoted) = constant.literal.val {
10799
assert!(promoted.is_none());
108100
// Don't peek inside trait associated constants.
109-
if cx.tcx.trait_of_item(def_id).is_some() {
110-
Self::in_any_value_of_ty(cx, constant.literal.ty)
111-
} else {
101+
if cx.tcx.trait_of_item(def_id).is_none() {
112102
let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def_id);
113-
let qualif = Self::in_qualifs(&qualifs);
103+
if !Self::in_qualifs(&qualifs) {
104+
return false;
105+
}
114106

115107
// Just in case the type is more specific than
116108
// the definition, e.g., impl associated const
117109
// with type parameters, take it into account.
118-
qualif && Self::in_any_value_of_ty(cx, constant.literal.ty)
119110
}
120-
} else {
121-
false
122111
}
112+
// Otherwise use the qualifs of the type.
113+
Self::in_any_value_of_ty(cx, constant.literal.ty)
123114
}
124115
}
125116
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(no_core, lang_items)]
2-
#![crate_type="rlib"]
2+
#![crate_type = "rlib"]
33
#![no_core]
44

55
pub static STATIC_BOOL: bool = true;
@@ -9,7 +9,6 @@ pub static mut STATIC_MUT_BOOL: bool = true;
99
const CONST_BOOL: bool = true;
1010
pub static CONST_BOOL_REF: &'static bool = &CONST_BOOL;
1111

12-
1312
#[lang = "sized"]
1413
trait Sized {}
1514

@@ -19,10 +18,13 @@ trait Copy {}
1918
#[lang = "freeze"]
2019
trait Freeze {}
2120

21+
// No `UnsafeCell`, so everything is `Freeze`.
22+
impl<T: ?Sized> Freeze for T {}
23+
2224
#[lang = "sync"]
2325
trait Sync {}
2426
impl Sync for bool {}
2527
impl Sync for &'static bool {}
2628

27-
#[lang="drop_in_place"]
28-
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) { }
29+
#[lang = "drop_in_place"]
30+
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}

0 commit comments

Comments
 (0)