Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7139b88

Browse files
committedMar 21, 2025
Auto merge of rust-lang#138785 - lcnr:typing-mode-borrowck, r=<try>
add `TypingMode::Borrowck` Still not quite ready Based on rust-lang#138492 and rust-lang#138719 r? `@compiler-errors` `@oli-obk`
2 parents 4ac032f + 674c2f4 commit 7139b88

File tree

208 files changed

+1953
-2657
lines changed

Some content is hidden

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

208 files changed

+1953
-2657
lines changed
 

‎compiler/rustc_borrowck/src/nll.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ use std::str::FromStr;
66
use std::{env, io};
77

88
use polonius_engine::{Algorithm, Output};
9-
use rustc_data_structures::fx::FxIndexMap;
10-
use rustc_hir::def_id::LocalDefId;
119
use rustc_index::IndexSlice;
1210
use rustc_middle::mir::pretty::{PrettyPrintMirOptions, dump_mir_with_options};
1311
use rustc_middle::mir::{
1412
Body, ClosureOutlivesSubject, ClosureRegionRequirements, PassWhere, Promoted, create_dump_file,
1513
dump_enabled, dump_mir,
1614
};
1715
use rustc_middle::ty::print::with_no_trimmed_paths;
18-
use rustc_middle::ty::{self, OpaqueHiddenType, TyCtxt};
16+
use rustc_middle::ty::{self, TyCtxt};
1917
use rustc_mir_dataflow::ResultsCursor;
2018
use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
2119
use rustc_mir_dataflow::move_paths::MoveData;
@@ -27,6 +25,7 @@ use tracing::{debug, instrument};
2725
use crate::borrow_set::BorrowSet;
2826
use crate::consumers::ConsumerOptions;
2927
use crate::diagnostics::{BorrowckDiagnosticsBuffer, RegionErrors};
28+
use crate::opaque_types::ConcreteOpaqueTypes;
3029
use crate::polonius::PoloniusDiagnosticsContext;
3130
use crate::polonius::legacy::{
3231
PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
@@ -40,7 +39,7 @@ use crate::{BorrowckInferCtxt, polonius, renumber};
4039
/// closure requirements to propagate, and any generated errors.
4140
pub(crate) struct NllOutput<'tcx> {
4241
pub regioncx: RegionInferenceContext<'tcx>,
43-
pub opaque_type_values: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
42+
pub concrete_opaque_types: ConcreteOpaqueTypes<'tcx>,
4443
pub polonius_input: Option<Box<PoloniusFacts>>,
4544
pub polonius_output: Option<Box<PoloniusOutput>>,
4645
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
@@ -99,6 +98,8 @@ pub(crate) fn compute_regions<'a, 'tcx>(
9998

10099
let location_map = Rc::new(DenseLocationMap::new(body));
101100

101+
let mut concrete_opaque_types = ConcreteOpaqueTypes::default();
102+
102103
// Run the MIR type-checker.
103104
let MirTypeckResults {
104105
constraints,
@@ -116,6 +117,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
116117
flow_inits,
117118
move_data,
118119
Rc::clone(&location_map),
120+
&mut concrete_opaque_types,
119121
);
120122

121123
// Create the region inference context, taking ownership of the
@@ -180,11 +182,11 @@ pub(crate) fn compute_regions<'a, 'tcx>(
180182
infcx.set_tainted_by_errors(guar);
181183
}
182184

183-
let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values);
185+
regioncx.infer_opaque_types(infcx, opaque_type_values, &mut concrete_opaque_types);
184186

185187
NllOutput {
186188
regioncx,
187-
opaque_type_values: remapped_opaque_tys,
189+
concrete_opaque_types,
188190
polonius_input: polonius_facts.map(Box::new),
189191
polonius_output,
190192
opt_closure_req: closure_region_requirements,
@@ -300,7 +302,7 @@ pub(super) fn dump_annotation<'tcx, 'infcx>(
300302
body: &Body<'tcx>,
301303
regioncx: &RegionInferenceContext<'tcx>,
302304
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
303-
opaque_type_values: &FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
305+
concrete_opaque_types: &ConcreteOpaqueTypes<'tcx>,
304306
diagnostics_buffer: &mut BorrowckDiagnosticsBuffer<'infcx, 'tcx>,
305307
) {
306308
let tcx = infcx.tcx;
@@ -343,8 +345,8 @@ pub(super) fn dump_annotation<'tcx, 'infcx>(
343345
err
344346
};
345347

346-
if !opaque_type_values.is_empty() {
347-
err.note(format!("Inferred opaque type values:\n{opaque_type_values:#?}"));
348+
if !concrete_opaque_types.is_empty() {
349+
err.note(format!("Inferred opaque type values:\n{concrete_opaque_types:#?}"));
348350
}
349351

350352
diagnostics_buffer.buffer_non_error(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use rustc_data_structures::fx::FxIndexMap;
2+
use rustc_hir::def_id::LocalDefId;
3+
use rustc_middle::ty::{OpaqueHiddenType, Ty, TyCtxt};
4+
5+
#[derive(Debug, Default)]
6+
pub(super) struct ConcreteOpaqueTypes<'tcx> {
7+
concrete_opaque_types: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
8+
}
9+
10+
impl<'tcx> ConcreteOpaqueTypes<'tcx> {
11+
pub(super) fn is_empty(&self) -> bool {
12+
self.concrete_opaque_types.is_empty()
13+
}
14+
15+
pub(super) fn into_inner(self) -> FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> {
16+
self.concrete_opaque_types
17+
}
18+
19+
/// Insert an opaque type into the list of opaque types defined by this function
20+
/// after mapping the hidden type to the generic parameters of the opaque type
21+
/// definition.
22+
pub(super) fn insert(
23+
&mut self,
24+
tcx: TyCtxt<'tcx>,
25+
def_id: LocalDefId,
26+
hidden_ty: OpaqueHiddenType<'tcx>,
27+
) {
28+
// Sometimes two opaque types are the same only after we remap the generic parameters
29+
// back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to
30+
// `(X, Y)` and `OpaqueType<Y, X>` mapped to `(Y, X)`, and those are the same, but we
31+
// only know that once we convert the generic parameters to those of the opaque type.
32+
if let Some(prev) = self.concrete_opaque_types.get_mut(&def_id) {
33+
if prev.ty != hidden_ty.ty {
34+
let (Ok(guar) | Err(guar)) =
35+
prev.build_mismatch_error(&hidden_ty, tcx).map(|d| d.emit());
36+
prev.ty = Ty::new_error(tcx, guar);
37+
}
38+
// Pick a better span if there is one.
39+
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
40+
prev.span = prev.span.substitute_dummy(hidden_ty.span);
41+
} else {
42+
self.concrete_opaque_types.insert(def_id, hidden_ty);
43+
}
44+
}
45+
46+
pub(super) fn extend_from_nested_body(
47+
&mut self,
48+
tcx: TyCtxt<'tcx>,
49+
nested_body: &FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
50+
) {
51+
for (&def_id, &hidden_ty) in nested_body {
52+
self.insert(tcx, def_id, hidden_ty);
53+
}
54+
}
55+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.