Skip to content

Commit 7b80539

Browse files
committed
Auto merge of #72010 - Dylan-DPC:rollup-prdj0pk, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #71989 (Use a single enum for the kind of a const context) - #71993 (Remove old `util/liveness.rs` module) - #71999 (Add myself to mailmap.) - #72001 (Adjust cfg(version) to lang team decision) - #72007 (Fix some tests failing in `--pass check` mode) - #72008 (Add const-generics test) Failed merges: r? @ghost
2 parents a51e004 + 6789540 commit 7b80539

File tree

24 files changed

+235
-476
lines changed

24 files changed

+235
-476
lines changed

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Carol (Nichols || Goulding) <[email protected]> <193874+carols10cents@user
4949
Carol (Nichols || Goulding) <[email protected]> <[email protected]>
5050
Carol (Nichols || Goulding) <[email protected]> <[email protected]>
5151
Carol Willing <[email protected]>
52+
Charles Lew <[email protected]> CrLF0710 <[email protected]>
5253
Chris C Cerami <[email protected]> Chris C Cerami <[email protected]>
5354
Chris Pressey <[email protected]>
5455
Chris Thorn <[email protected]> Chris Thorn <[email protected]>

src/librustc_attr/build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fn main() {
22
println!("cargo:rerun-if-changed=build.rs");
3-
println!("cargo:rerun-if-env-changed=CFG_VERSION");
3+
println!("cargo:rerun-if-env-changed=CFG_RELEASE");
4+
println!("cargo:rerun-if-env-changed=CFG_RELEASE_CHANNEL");
45
}

src/librustc_attr/builtin.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,12 @@ pub fn eval_condition(
652652
return false;
653653
}
654654
};
655-
let version = Version::parse(env!("CFG_VERSION")).unwrap();
655+
let channel = env!("CFG_RELEASE_CHANNEL");
656+
let nightly = channel == "nightly" || channel == "dev";
657+
let rustc_version = Version::parse(env!("CFG_RELEASE")).unwrap();
656658

657-
version >= min_version
659+
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-625474439 for details
660+
if nightly { rustc_version > min_version } else { rustc_version >= min_version }
658661
}
659662
ast::MetaItemKind::List(ref mis) => {
660663
for mi in mis.iter() {

src/librustc_hir/hir.rs

+47
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,53 @@ impl BodyOwnerKind {
12911291
}
12921292
}
12931293

1294+
/// The kind of an item that requires const-checking.
1295+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1296+
pub enum ConstContext {
1297+
/// A `const fn`.
1298+
ConstFn,
1299+
1300+
/// A `static` or `static mut`.
1301+
Static(Mutability),
1302+
1303+
/// A `const`, associated `const`, or other const context.
1304+
///
1305+
/// Other contexts include:
1306+
/// - Array length expressions
1307+
/// - Enum discriminants
1308+
/// - Const generics
1309+
///
1310+
/// For the most part, other contexts are treated just like a regular `const`, so they are
1311+
/// lumped into the same category.
1312+
Const,
1313+
}
1314+
1315+
impl ConstContext {
1316+
/// A description of this const context that can appear between backticks in an error message.
1317+
///
1318+
/// E.g. `const` or `static mut`.
1319+
pub fn keyword_name(self) -> &'static str {
1320+
match self {
1321+
Self::Const => "const",
1322+
Self::Static(Mutability::Not) => "static",
1323+
Self::Static(Mutability::Mut) => "static mut",
1324+
Self::ConstFn => "const fn",
1325+
}
1326+
}
1327+
}
1328+
1329+
/// A colloquial, trivially pluralizable description of this const context for use in error
1330+
/// messages.
1331+
impl fmt::Display for ConstContext {
1332+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1333+
match *self {
1334+
Self::Const => write!(f, "constant"),
1335+
Self::Static(_) => write!(f, "static"),
1336+
Self::ConstFn => write!(f, "constant function"),
1337+
}
1338+
}
1339+
}
1340+
12941341
/// A literal.
12951342
pub type Lit = Spanned<LitKind>;
12961343

src/librustc_middle/hir/map/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ impl<'hir> Map<'hir> {
408408
})
409409
}
410410

411+
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
412+
///
413+
/// Panics if `LocalDefId` does not have an associated body.
411414
pub fn body_owner_kind(&self, id: HirId) -> BodyOwnerKind {
412415
match self.get(id) {
413416
Node::Item(&Item { kind: ItemKind::Const(..), .. })
@@ -424,6 +427,23 @@ impl<'hir> Map<'hir> {
424427
}
425428
}
426429

430+
/// Returns the `ConstContext` of the body associated with this `LocalDefId`.
431+
///
432+
/// Panics if `LocalDefId` does not have an associated body.
433+
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
434+
let hir_id = self.local_def_id_to_hir_id(did);
435+
let ccx = match self.body_owner_kind(hir_id) {
436+
BodyOwnerKind::Const => ConstContext::Const,
437+
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
438+
439+
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
440+
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
441+
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
442+
};
443+
444+
Some(ccx)
445+
}
446+
427447
pub fn ty_param_owner(&self, id: HirId) -> HirId {
428448
match self.get(id) {
429449
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => id,
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use rustc_middle::mir::visit::{
2+
MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext,
3+
};
4+
5+
#[derive(Eq, PartialEq, Clone)]
6+
pub enum DefUse {
7+
Def,
8+
Use,
9+
Drop,
10+
}
11+
12+
pub fn categorize(context: PlaceContext) -> Option<DefUse> {
13+
match context {
14+
///////////////////////////////////////////////////////////////////////////
15+
// DEFS
16+
17+
PlaceContext::MutatingUse(MutatingUseContext::Store) |
18+
19+
// This is potentially both a def and a use...
20+
PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) |
21+
22+
// We let Call define the result in both the success and
23+
// unwind cases. This is not really correct, however it
24+
// does not seem to be observable due to the way that we
25+
// generate MIR. To do things properly, we would apply
26+
// the def in call only to the input from the success
27+
// path and not the unwind path. -nmatsakis
28+
PlaceContext::MutatingUse(MutatingUseContext::Call) |
29+
PlaceContext::MutatingUse(MutatingUseContext::Yield) |
30+
31+
// Storage live and storage dead aren't proper defines, but we can ignore
32+
// values that come before them.
33+
PlaceContext::NonUse(NonUseContext::StorageLive) |
34+
PlaceContext::NonUse(NonUseContext::StorageDead) => Some(DefUse::Def),
35+
36+
///////////////////////////////////////////////////////////////////////////
37+
// REGULAR USES
38+
//
39+
// These are uses that occur *outside* of a drop. For the
40+
// purposes of NLL, these are special in that **all** the
41+
// lifetimes appearing in the variable must be live for each regular use.
42+
43+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) |
44+
PlaceContext::MutatingUse(MutatingUseContext::Projection) |
45+
46+
// Borrows only consider their local used at the point of the borrow.
47+
// This won't affect the results since we use this analysis for generators
48+
// and we only care about the result at suspension points. Borrows cannot
49+
// cross suspension points so this behavior is unproblematic.
50+
PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
51+
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
52+
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
53+
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
54+
55+
PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
56+
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |
57+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
58+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
59+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
60+
PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
61+
PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
62+
Some(DefUse::Use),
63+
64+
///////////////////////////////////////////////////////////////////////////
65+
// DROP USES
66+
//
67+
// These are uses that occur in a DROP (a MIR drop, not a
68+
// call to `std::mem::drop()`). For the purposes of NLL,
69+
// uses in drop are special because `#[may_dangle]`
70+
// attributes can affect whether lifetimes must be live.
71+
72+
PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
73+
Some(DefUse::Drop),
74+
75+
// Debug info is neither def nor use.
76+
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,
77+
}
78+
}

src/librustc_mir/borrow_check/diagnostics/find_use.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::collections::VecDeque;
22
use std::rc::Rc;
33

44
use crate::borrow_check::{
5+
def_use::{self, DefUse},
56
nll::ToRegionVid,
67
region_infer::{Cause, RegionInferenceContext},
78
};
8-
use crate::util::liveness::{self, DefUse};
99
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
1111
use rustc_middle::mir::{Body, Local, Location};
@@ -117,7 +117,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for DefUseVisitor<'cx, 'tcx> {
117117
});
118118

119119
if found_it {
120-
self.def_use_result = match liveness::categorize(context) {
120+
self.def_use_result = match def_use::categorize(context) {
121121
Some(DefUse::Def) => Some(DefUseResult::Def),
122122
Some(DefUse::Use) => Some(DefUseResult::UseLive { local }),
123123
Some(DefUse::Drop) => Some(DefUseResult::UseDrop { local }),

src/librustc_mir/borrow_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use self::path_utils::*;
5151
mod borrow_set;
5252
mod constraint_generation;
5353
mod constraints;
54+
mod def_use;
5455
mod diagnostics;
5556
mod facts;
5657
mod invalidation;

src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use rustc_index::vec::IndexVec;
33
use rustc_middle::mir::visit::{PlaceContext, Visitor};
44
use rustc_middle::mir::{Body, Local, Location};
55

6-
use crate::util::liveness::{categorize, DefUse};
7-
6+
use crate::borrow_check::def_use::{self, DefUse};
87
use crate::borrow_check::region_infer::values::{PointIndex, RegionValueElements};
98

109
/// A map that cross references each local with the locations where it
@@ -160,7 +159,7 @@ impl LocalUseMapBuild<'_> {
160159
impl Visitor<'tcx> for LocalUseMapBuild<'_> {
161160
fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
162161
if self.locals_with_use_data[local] {
163-
match categorize(context) {
162+
match def_use::categorize(context) {
164163
Some(DefUse::Def) => self.insert_def(local, location),
165164
Some(DefUse::Use) => self.insert_use(local, location),
166165
Some(DefUse::Drop) => self.insert_drop(local, location),

src/librustc_mir/borrow_check/type_check/liveness/polonius.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::borrow_check::def_use::{self, DefUse};
12
use crate::borrow_check::location::{LocationIndex, LocationTable};
23
use crate::dataflow::indexes::MovePathIndex;
34
use crate::dataflow::move_paths::{LookupResult, MoveData};
4-
use crate::util::liveness::{categorize, DefUse};
55
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
66
use rustc_middle::mir::{Body, Local, Location, Place};
77
use rustc_middle::ty::subst::GenericArg;
@@ -56,7 +56,7 @@ impl UseFactsExtractor<'_> {
5656

5757
impl Visitor<'tcx> for UseFactsExtractor<'_> {
5858
fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
59-
match categorize(context) {
59+
match def_use::categorize(context) {
6060
Some(DefUse::Def) => self.insert_def(local, location),
6161
Some(DefUse::Use) => self.insert_use(local, location),
6262
Some(DefUse::Drop) => self.insert_drop_use(local, location),

src/librustc_mir/dataflow/impls/liveness.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use crate::dataflow::{AnalysisDomain, Backward, BottomValue, GenKill, GenKillAna
66

77
/// A [live-variable dataflow analysis][liveness].
88
///
9+
/// This analysis considers references as being used only at the point of the
10+
/// borrow. In other words, this analysis does not track uses because of references that already
11+
/// exist. See [this `mir-datalow` test][flow-test] for an example. You almost never want to use
12+
/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
13+
///
14+
/// [`MaybeBorrowedLocals`]: ../struct.MaybeBorrowedLocals.html
15+
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
916
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
1017
pub struct MaybeLiveLocals;
1118

src/librustc_mir/transform/check_consts/mod.rs

+3-65
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_middle::mir;
1010
use rustc_middle::ty::{self, TyCtxt};
1111

12-
use std::fmt;
13-
1412
pub use self::qualifs::Qualif;
1513

1614
mod ops;
@@ -25,7 +23,7 @@ pub struct ConstCx<'mir, 'tcx> {
2523
pub tcx: TyCtxt<'tcx>,
2624
pub def_id: DefId,
2725
pub param_env: ty::ParamEnv<'tcx>,
28-
pub const_kind: Option<ConstKind>,
26+
pub const_kind: Option<hir::ConstContext>,
2927
}
3028

3129
impl ConstCx<'mir, 'tcx> {
@@ -40,78 +38,18 @@ impl ConstCx<'mir, 'tcx> {
4038
body: &'mir mir::Body<'tcx>,
4139
param_env: ty::ParamEnv<'tcx>,
4240
) -> Self {
43-
let const_kind = ConstKind::for_item(tcx, def_id);
44-
41+
let const_kind = tcx.hir().body_const_context(def_id);
4542
ConstCx { body, tcx, def_id: def_id.to_def_id(), param_env, const_kind }
4643
}
4744

4845
/// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
4946
///
5047
/// Panics if this `Item` is not const.
51-
pub fn const_kind(&self) -> ConstKind {
48+
pub fn const_kind(&self) -> hir::ConstContext {
5249
self.const_kind.expect("`const_kind` must not be called on a non-const fn")
5350
}
5451
}
5552

56-
/// The kinds of items which require compile-time evaluation.
57-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
58-
pub enum ConstKind {
59-
/// A `static` item.
60-
Static,
61-
/// A `static mut` item.
62-
StaticMut,
63-
/// A `const fn` item.
64-
ConstFn,
65-
/// A `const` item or an anonymous constant (e.g. in array lengths).
66-
Const,
67-
}
68-
69-
impl ConstKind {
70-
/// Returns the validation mode for the item with the given `DefId`, or `None` if this item
71-
/// does not require validation (e.g. a non-const `fn`).
72-
pub fn for_item(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option<Self> {
73-
use hir::BodyOwnerKind as HirKind;
74-
75-
let hir_id = tcx.hir().as_local_hir_id(def_id);
76-
77-
let mode = match tcx.hir().body_owner_kind(hir_id) {
78-
HirKind::Closure => return None,
79-
80-
// Note: this is deliberately checking for `is_const_fn_raw`, as the `is_const_fn`
81-
// checks take into account the `rustc_const_unstable` attribute combined with enabled
82-
// feature gates. Otherwise, const qualification would _not check_ whether this
83-
// function body follows the `const fn` rules, as an unstable `const fn` would
84-
// be considered "not const". More details are available in issue #67053.
85-
HirKind::Fn if tcx.is_const_fn_raw(def_id) => ConstKind::ConstFn,
86-
HirKind::Fn => return None,
87-
88-
HirKind::Const => ConstKind::Const,
89-
90-
HirKind::Static(hir::Mutability::Not) => ConstKind::Static,
91-
HirKind::Static(hir::Mutability::Mut) => ConstKind::StaticMut,
92-
};
93-
94-
Some(mode)
95-
}
96-
97-
pub fn is_static(self) -> bool {
98-
match self {
99-
ConstKind::Static | ConstKind::StaticMut => true,
100-
ConstKind::ConstFn | ConstKind::Const => false,
101-
}
102-
}
103-
}
104-
105-
impl fmt::Display for ConstKind {
106-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107-
match *self {
108-
ConstKind::Const => write!(f, "constant"),
109-
ConstKind::Static | ConstKind::StaticMut => write!(f, "static"),
110-
ConstKind::ConstFn => write!(f, "constant function"),
111-
}
112-
}
113-
}
114-
11553
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
11654
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
11755
Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()

0 commit comments

Comments
 (0)