Skip to content

Commit 9aa706d

Browse files
author
Alexander Regueiro
committed
Simplify const_qualif propagation of const fn calls.
1 parent 2ba3d49 commit 9aa706d

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bitflags! {
4545
// Borrows of temporaries can be promoted only if
4646
// they have none of these qualifications.
4747
struct Qualif: u8 {
48-
// Constant containing interior mutability (UnsafeCell).
48+
// Constant containing interior mutability (`UnsafeCell`).
4949
const MUTABLE_INTERIOR = 1 << 0;
5050

5151
// Constant containing an ADT that implements Drop.
@@ -62,11 +62,12 @@ impl<'a, 'tcx> Qualif {
6262
fn restrict(&mut self, ty: Ty<'tcx>,
6363
tcx: TyCtxt<'a, 'tcx, 'tcx>,
6464
param_env: ty::ParamEnv<'tcx>) {
65+
self.insert(Qualif::MUTABLE_INTERIOR | Qualif::NEEDS_DROP);
6566
if ty.is_freeze(tcx, param_env, DUMMY_SP) {
66-
*self = *self - Qualif::MUTABLE_INTERIOR;
67+
self.remove(Qualif::MUTABLE_INTERIOR);
6768
}
6869
if !ty.needs_drop(tcx, param_env) {
69-
*self = *self - Qualif::NEEDS_DROP;
70+
self.remove(Qualif::NEEDS_DROP);
7071
}
7172
}
7273
}
@@ -183,18 +184,17 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
183184
}
184185
}
185186

186-
/// Add the given qualification to self.qualif.
187+
/// Add the given qualification to `self.qualif`.
187188
fn add(&mut self, qualif: Qualif) {
188189
self.qualif = self.qualif | qualif;
189190
}
190191

191-
/// Add the given type's qualification to self.qualif.
192-
fn add_type(&mut self, ty: Ty<'tcx>) {
193-
self.add(Qualif::MUTABLE_INTERIOR | Qualif::NEEDS_DROP);
192+
/// Restrict `self.qualif` by the given type's qualification.
193+
fn restrict_to_type(&mut self, ty: Ty<'tcx>) {
194194
self.qualif.restrict(ty, self.tcx, self.param_env);
195195
}
196196

197-
/// Within the provided closure, self.qualif will start
197+
/// Within the provided closure, `self.qualif` will start
198198
/// out empty, and its value after the closure returns will
199199
/// be combined with the value before the call to nest.
200200
fn nest<F: FnOnce(&mut Self)>(&mut self, f: F) {
@@ -501,7 +501,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
501501
}
502502

503503
let ty = place.ty(this.mir, this.tcx).to_ty(this.tcx);
504-
this.qualif.restrict(ty, this.tcx, this.param_env);
504+
this.restrict_to_type(ty);
505505
}
506506

507507
ProjectionElem::ConstantIndex {..} |
@@ -534,7 +534,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
534534
} = constant.literal {
535535
// Don't peek inside trait associated constants.
536536
if self.tcx.trait_of_item(def_id).is_some() {
537-
self.add_type(ty);
537+
self.restrict_to_type(ty);
538538
} else {
539539
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);
540540

@@ -936,16 +936,9 @@ This does not pose a problem by itself because they can't be accessed directly."
936936
}
937937

938938
if let Some((ref dest, _)) = *destination {
939-
// Avoid propagating irrelevant callee/argument qualifications.
940-
if self.qualif.intersects(Qualif::NOT_CONST) {
941-
self.qualif = Qualif::NOT_CONST;
942-
} else {
943-
// Be conservative about the returned value of a const fn.
944-
let tcx = self.tcx;
945-
let ty = dest.ty(self.mir, tcx).to_ty(tcx);
946-
self.qualif = Qualif::empty();
947-
self.add_type(ty);
948-
}
939+
// Be conservative about the returned value of a const fn.
940+
let ty = dest.ty(self.mir, self.tcx).to_ty(self.tcx);
941+
self.restrict_to_type(ty);
949942
self.assign(dest, location);
950943
}
951944
} else if let TerminatorKind::Drop { location: ref place, .. } = *kind {

0 commit comments

Comments
 (0)