Skip to content

Commit 7664dfe

Browse files
committed
Auto merge of rust-lang#111925 - Manishearth:rollup-z6z6l2v, r=Manishearth
Rollup of 5 pull requests Successful merges: - rust-lang#111741 (Use `ObligationCtxt` in custom type ops) - rust-lang#111840 (Expose more information in `get_body_with_borrowck_facts`) - rust-lang#111876 (Roll compiler_builtins to 0.1.92) - rust-lang#111912 (Use `Option::is_some_and` and `Result::is_ok_and` in the compiler ) - rust-lang#111915 (libtest: Improve error when missing `-Zunstable-options`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 096309e + 8038606 commit 7664dfe

File tree

115 files changed

+417
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+417
-378
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ dependencies = [
726726

727727
[[package]]
728728
name = "compiler_builtins"
729-
version = "0.1.91"
729+
version = "0.1.92"
730730
source = "registry+https://github.com/rust-lang/crates.io-index"
731-
checksum = "571298a3cce7e2afbd3d61abb91a18667d5ab25993ec577a88ee8ac45f00cc3a"
731+
checksum = "64518f1ae689f74db058bbfb3238dfe6eb53f59f4ae712f1ff4348628522e190"
732732
dependencies = [
733733
"cc",
734734
"rustc-std-workspace-core",

compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2391,10 +2391,10 @@ pub struct FnDecl {
23912391

23922392
impl FnDecl {
23932393
pub fn has_self(&self) -> bool {
2394-
self.inputs.get(0).map_or(false, Param::is_self)
2394+
self.inputs.get(0).is_some_and(Param::is_self)
23952395
}
23962396
pub fn c_variadic(&self) -> bool {
2397-
self.inputs.last().map_or(false, |arg| matches!(arg.ty.kind, TyKind::CVarArgs))
2397+
self.inputs.last().is_some_and(|arg| matches!(arg.ty.kind, TyKind::CVarArgs))
23982398
}
23992399
}
24002400

compiler/rustc_ast/src/attr/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl Attribute {
149149
}
150150

151151
pub fn may_have_doc_links(&self) -> bool {
152-
self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
152+
self.doc_str().is_some_and(|s| comments::may_have_doc_links(s.as_str()))
153153
}
154154

155155
pub fn is_proc_macro_attr(&self) -> bool {
@@ -441,12 +441,12 @@ impl NestedMetaItem {
441441

442442
/// Returns `true` if this list item is a MetaItem with a name of `name`.
443443
pub fn has_name(&self, name: Symbol) -> bool {
444-
self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
444+
self.meta_item().is_some_and(|meta_item| meta_item.has_name(name))
445445
}
446446

447447
/// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
448448
pub fn is_word(&self) -> bool {
449-
self.meta_item().map_or(false, |meta_item| meta_item.is_word())
449+
self.meta_item().is_some_and(|meta_item| meta_item.is_word())
450450
}
451451

452452
/// Gets a list of inner meta items from a list `MetaItem` type.

compiler/rustc_ast/src/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl Token {
607607
/// Returns `true` if the token is an identifier whose name is the given
608608
/// string slice.
609609
pub fn is_ident_named(&self, name: Symbol) -> bool {
610-
self.ident().map_or(false, |(ident, _)| ident.name == name)
610+
self.ident().is_some_and(|(ident, _)| ident.name == name)
611611
}
612612

613613
/// Returns `true` if the token is an interpolated path.

compiler/rustc_ast/src/util/literal.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
392392
// Small bases are lexed as if they were base 10, e.g, the string
393393
// might be `0b10201`. This will cause the conversion above to fail,
394394
// but these kinds of errors are already reported by the lexer.
395-
let from_lexer =
396-
base < 10 && s.chars().any(|c| c.to_digit(10).map_or(false, |d| d >= base));
395+
let from_lexer = base < 10 && s.chars().any(|c| c.to_digit(10).is_some_and(|d| d >= base));
397396
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge(base) }
398397
})
399398
}

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'a> AstValidator<'a> {
348348
let source_map = self.session.source_map();
349349
let end = source_map.end_point(sp);
350350

351-
if source_map.span_to_snippet(end).map(|s| s == ";").unwrap_or(false) {
351+
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
352352
end
353353
} else {
354354
sp.shrink_to_hi()

compiler/rustc_ast_passes/src/feature_gate.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
317317
match i.kind {
318318
ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
319319
let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
320-
let links_to_llvm =
321-
link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
320+
let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
322321
if links_to_llvm {
323322
gate_feature_post!(
324323
&self,

compiler/rustc_borrowck/src/borrow_set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct BorrowSet<'tcx> {
3030
/// Map from local to all the borrows on that local.
3131
pub local_map: FxIndexMap<mir::Local, FxIndexSet<BorrowIndex>>,
3232

33-
pub(crate) locals_state_at_exit: LocalsStateAtExit,
33+
pub locals_state_at_exit: LocalsStateAtExit,
3434
}
3535

3636
impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
@@ -153,7 +153,7 @@ impl<'tcx> BorrowSet<'tcx> {
153153
self.activation_map.get(&location).map_or(&[], |activations| &activations[..])
154154
}
155155

156-
pub(crate) fn len(&self) -> usize {
156+
pub fn len(&self) -> usize {
157157
self.location_map.len()
158158
}
159159

compiler/rustc_borrowck/src/consumers.rs

+86-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,95 @@
33
//! This file provides API for compiler consumers.
44
55
use rustc_hir::def_id::LocalDefId;
6-
use rustc_index::IndexSlice;
6+
use rustc_index::{IndexSlice, IndexVec};
77
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
8-
use rustc_middle::mir::Body;
8+
use rustc_middle::mir::{Body, Promoted};
99
use rustc_middle::ty::TyCtxt;
10+
use std::rc::Rc;
11+
12+
use crate::borrow_set::BorrowSet;
1013

1114
pub use super::{
15+
constraints::OutlivesConstraint,
16+
dataflow::{calculate_borrows_out_of_scope_at_location, BorrowIndex, Borrows},
1217
facts::{AllFacts as PoloniusInput, RustcFacts},
1318
location::{LocationTable, RichLocation},
1419
nll::PoloniusOutput,
15-
BodyWithBorrowckFacts,
20+
place_ext::PlaceExt,
21+
places_conflict::{places_conflict, PlaceConflictBias},
22+
region_infer::RegionInferenceContext,
1623
};
1724

18-
/// This function computes Polonius facts for the given body. It makes a copy of
19-
/// the body because it needs to regenerate the region identifiers. This function
20-
/// should never be invoked during a typical compilation session due to performance
21-
/// issues with Polonius.
25+
/// Options determining the output behavior of [`get_body_with_borrowck_facts`].
26+
///
27+
/// If executing under `-Z polonius` the choice here has no effect, and everything as if
28+
/// [`PoloniusOutputFacts`](ConsumerOptions::PoloniusOutputFacts) had been selected
29+
/// will be retrieved.
30+
#[derive(Debug, Copy, Clone)]
31+
pub enum ConsumerOptions {
32+
/// Retrieve the [`Body`] along with the [`BorrowSet`](super::borrow_set::BorrowSet)
33+
/// and [`RegionInferenceContext`]. If you would like the body only, use
34+
/// [`TyCtxt::mir_promoted`].
35+
///
36+
/// These can be used in conjunction with [`calculate_borrows_out_of_scope_at_location`].
37+
RegionInferenceContext,
38+
/// The recommended option. Retrieves the maximal amount of information
39+
/// without significant slowdowns.
40+
///
41+
/// Implies [`RegionInferenceContext`](ConsumerOptions::RegionInferenceContext),
42+
/// and additionally retrieve the [`LocationTable`] and [`PoloniusInput`] that
43+
/// would be given to Polonius. Critically, this does not run Polonius, which
44+
/// one may want to avoid due to performance issues on large bodies.
45+
PoloniusInputFacts,
46+
/// Implies [`PoloniusInputFacts`](ConsumerOptions::PoloniusInputFacts),
47+
/// and additionally runs Polonius to calculate the [`PoloniusOutput`].
48+
PoloniusOutputFacts,
49+
}
50+
51+
impl ConsumerOptions {
52+
/// Should the Polonius input facts be computed?
53+
pub(crate) fn polonius_input(&self) -> bool {
54+
matches!(self, Self::PoloniusInputFacts | Self::PoloniusOutputFacts)
55+
}
56+
/// Should we run Polonius and collect the output facts?
57+
pub(crate) fn polonius_output(&self) -> bool {
58+
matches!(self, Self::PoloniusOutputFacts)
59+
}
60+
}
61+
62+
/// A `Body` with information computed by the borrow checker. This struct is
63+
/// intended to be consumed by compiler consumers.
64+
///
65+
/// We need to include the MIR body here because the region identifiers must
66+
/// match the ones in the Polonius facts.
67+
pub struct BodyWithBorrowckFacts<'tcx> {
68+
/// A mir body that contains region identifiers.
69+
pub body: Body<'tcx>,
70+
/// The mir bodies of promoteds.
71+
pub promoted: IndexVec<Promoted, Body<'tcx>>,
72+
/// The set of borrows occurring in `body` with data about them.
73+
pub borrow_set: Rc<BorrowSet<'tcx>>,
74+
/// Context generated during borrowck, intended to be passed to
75+
/// [`calculate_borrows_out_of_scope_at_location`].
76+
pub region_inference_context: Rc<RegionInferenceContext<'tcx>>,
77+
/// The table that maps Polonius points to locations in the table.
78+
/// Populated when using [`ConsumerOptions::PoloniusInputFacts`]
79+
/// or [`ConsumerOptions::PoloniusOutputFacts`].
80+
pub location_table: Option<LocationTable>,
81+
/// Polonius input facts.
82+
/// Populated when using [`ConsumerOptions::PoloniusInputFacts`]
83+
/// or [`ConsumerOptions::PoloniusOutputFacts`].
84+
pub input_facts: Option<Box<PoloniusInput>>,
85+
/// Polonius output facts. Populated when using
86+
/// [`ConsumerOptions::PoloniusOutputFacts`].
87+
pub output_facts: Option<Rc<PoloniusOutput>>,
88+
}
89+
90+
/// This function computes borrowck facts for the given body. The [`ConsumerOptions`]
91+
/// determine which facts are returned. This function makes a copy of the body because
92+
/// it needs to regenerate the region identifiers. It should never be invoked during a
93+
/// typical compilation session due to the unnecessary overhead of returning
94+
/// [`BodyWithBorrowckFacts`].
2295
///
2396
/// Note:
2497
/// * This function will panic if the required body was already stolen. This
@@ -28,10 +101,14 @@ pub use super::{
28101
/// that shows how to do this at `tests/run-make/obtain-borrowck/`.
29102
///
30103
/// * Polonius is highly unstable, so expect regular changes in its signature or other details.
31-
pub fn get_body_with_borrowck_facts(tcx: TyCtxt<'_>, def: LocalDefId) -> BodyWithBorrowckFacts<'_> {
104+
pub fn get_body_with_borrowck_facts(
105+
tcx: TyCtxt<'_>,
106+
def: LocalDefId,
107+
options: ConsumerOptions,
108+
) -> BodyWithBorrowckFacts<'_> {
32109
let (input_body, promoted) = tcx.mir_promoted(def);
33110
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def)).build();
34111
let input_body: &Body<'_> = &input_body.borrow();
35112
let promoted: &IndexSlice<_, _> = &promoted.borrow();
36-
*super::do_mir_borrowck(&infcx, input_body, promoted, true).1.unwrap()
113+
*super::do_mir_borrowck(&infcx, input_body, promoted, Some(options)).1.unwrap()
37114
}

compiler/rustc_borrowck/src/dataflow.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,32 @@ impl<'tcx> OutOfScopePrecomputer<'_, 'tcx> {
228228
}
229229
}
230230

231+
pub fn calculate_borrows_out_of_scope_at_location<'tcx>(
232+
body: &Body<'tcx>,
233+
regioncx: &RegionInferenceContext<'tcx>,
234+
borrow_set: &BorrowSet<'tcx>,
235+
) -> FxIndexMap<Location, Vec<BorrowIndex>> {
236+
let mut prec = OutOfScopePrecomputer::new(body, regioncx);
237+
for (borrow_index, borrow_data) in borrow_set.iter_enumerated() {
238+
let borrow_region = borrow_data.region;
239+
let location = borrow_data.reserve_location;
240+
241+
prec.precompute_borrows_out_of_scope(borrow_index, borrow_region, location);
242+
}
243+
244+
prec.borrows_out_of_scope_at_location
245+
}
246+
231247
impl<'a, 'tcx> Borrows<'a, 'tcx> {
232-
pub(crate) fn new(
248+
pub fn new(
233249
tcx: TyCtxt<'tcx>,
234250
body: &'a Body<'tcx>,
235251
nonlexical_regioncx: &'a RegionInferenceContext<'tcx>,
236252
borrow_set: &'a BorrowSet<'tcx>,
237253
) -> Self {
238-
let mut prec = OutOfScopePrecomputer::new(body, nonlexical_regioncx);
239-
for (borrow_index, borrow_data) in borrow_set.iter_enumerated() {
240-
let borrow_region = borrow_data.region;
241-
let location = borrow_data.reserve_location;
242-
243-
prec.precompute_borrows_out_of_scope(borrow_index, borrow_region, location);
244-
}
245-
246-
Borrows {
247-
tcx,
248-
body,
249-
borrow_set,
250-
borrows_out_of_scope_at_location: prec.borrows_out_of_scope_at_location,
251-
}
254+
let borrows_out_of_scope_at_location =
255+
calculate_borrows_out_of_scope_at_location(body, nonlexical_regioncx, borrow_set);
256+
Borrows { tcx, body, borrow_set, borrows_out_of_scope_at_location }
252257
}
253258

254259
pub fn location(&self, idx: BorrowIndex) -> &Location {

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'tcx> ToUniverseInfo<'tcx>
128128
}
129129
}
130130

131-
impl<'tcx, F, G> ToUniverseInfo<'tcx> for Canonical<'tcx, type_op::custom::CustomTypeOp<F, G>> {
131+
impl<'tcx, F> ToUniverseInfo<'tcx> for Canonical<'tcx, type_op::custom::CustomTypeOp<F>> {
132132
fn to_universe_info(self, _base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
133133
// We can't rerun custom type ops.
134134
UniverseInfo::other()

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
118118
let path_span = path_span.unwrap();
119119
// path_span is only present in the case of closure capture
120120
assert!(matches!(later_use_kind, LaterUseKind::ClosureCapture));
121-
if !borrow_span.map_or(false, |sp| sp.overlaps(var_or_use_span)) {
121+
if !borrow_span.is_some_and(|sp| sp.overlaps(var_or_use_span)) {
122122
let path_label = "used here by closure";
123123
let capture_kind_label = message;
124124
err.span_label(
@@ -224,12 +224,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
224224
if info.tail_result_is_ignored {
225225
// #85581: If the first mutable borrow's scope contains
226226
// the second borrow, this suggestion isn't helpful.
227-
if !multiple_borrow_span
228-
.map(|(old, new)| {
229-
old.to(info.span.shrink_to_hi()).contains(new)
230-
})
231-
.unwrap_or(false)
232-
{
227+
if !multiple_borrow_span.is_some_and(|(old, new)| {
228+
old.to(info.span.shrink_to_hi()).contains(new)
229+
}) {
233230
err.span_suggestion_verbose(
234231
info.span.shrink_to_hi(),
235232
"consider adding semicolon after the expression so its \

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11561156
ty::Adt(def, ..) => Some(def.did()),
11571157
_ => None,
11581158
});
1159-
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
1159+
let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
11601160
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
11611161
});
11621162
if is_option_or_result && maybe_reinitialized_locations_is_empty {

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
289289
.body
290290
.local_decls
291291
.get(local)
292-
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local]))
293-
.unwrap_or(false) =>
292+
.is_some_and(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) =>
294293
{
295294
let decl = &self.body.local_decls[local];
296295
err.span_label(span, format!("cannot {act}"));
@@ -443,7 +442,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
443442
.sess
444443
.source_map()
445444
.span_to_snippet(span)
446-
.map_or(false, |snippet| snippet.starts_with("&mut ")) =>
445+
.is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
447446
{
448447
err.span_label(span, format!("cannot {act}"));
449448
err.span_suggestion(

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ impl OutlivesSuggestionBuilder {
125125
|(r, _)| {
126126
self.constraints_to_add
127127
.get(r)
128-
.map(|r_outlived| r_outlived.as_slice().contains(fr))
129-
.unwrap_or(false)
128+
.is_some_and(|r_outlived| r_outlived.as_slice().contains(fr))
130129
},
131130
);
132131

0 commit comments

Comments
 (0)