Skip to main content

rustc_borrowck/
nll.rs

1//! The entry point of the NLL borrow checker.
2
3use std::io;
4use std::path::PathBuf;
5use std::rc::Rc;
6use std::str::FromStr;
7
8use polonius_engine::{Algorithm, AllFacts, Output};
9use rustc_data_structures::frozen::Frozen;
10use rustc_hir::attrs::AttributeKind;
11use rustc_hir::find_attr;
12use rustc_index::IndexSlice;
13use rustc_middle::mir::pretty::PrettyPrintMirOptions;
14use rustc_middle::mir::{Body, MirDumper, PassWhere, Promoted};
15use rustc_middle::ty::print::with_no_trimmed_paths;
16use rustc_middle::ty::{self, TyCtxt};
17use rustc_mir_dataflow::move_paths::MoveData;
18use rustc_mir_dataflow::points::DenseLocationMap;
19use rustc_session::config::MirIncludeSpans;
20use tracing::{debug, instrument};
21
22use crate::borrow_set::BorrowSet;
23use crate::consumers::RustcFacts;
24use crate::diagnostics::RegionErrors;
25use crate::handle_placeholders::compute_sccs_applying_placeholder_outlives_constraints;
26use crate::polonius::PoloniusContext;
27use crate::polonius::legacy::{
28    PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
29};
30use crate::region_infer::RegionInferenceContext;
31use crate::type_check::MirTypeckRegionConstraints;
32use crate::type_check::free_region_relations::UniversalRegionRelations;
33use crate::universal_regions::UniversalRegions;
34use crate::{
35    BorrowCheckRootCtxt, BorrowckInferCtxt, ClosureOutlivesSubject, ClosureRegionRequirements,
36    polonius, renumber,
37};
38
39/// The output of `nll::compute_regions`. This includes the computed `RegionInferenceContext`, any
40/// closure requirements to propagate, and any generated errors.
41pub(crate) struct NllOutput<'tcx> {
42    pub regioncx: RegionInferenceContext<'tcx>,
43    pub polonius_input: Option<Box<PoloniusFacts>>,
44    pub polonius_output: Option<Box<PoloniusOutput>>,
45    pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
46    pub nll_errors: RegionErrors<'tcx>,
47
48    /// When using `-Zpolonius=next`: the data used to compute errors and diagnostics, e.g.
49    /// localized typeck and liveness constraints.
50    pub polonius_context: Option<PoloniusContext>,
51}
52
53/// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal
54/// regions (e.g., region parameters) declared on the function. That set will need to be given to
55/// `compute_regions`.
56#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("replace_regions_in_mir",
                                    "rustc_borrowck::nll", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/nll.rs"),
                                    ::tracing_core::__macro_support::Option::Some(56u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::nll"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: UniversalRegions<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let def = body.source.def_id().expect_local();
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/nll.rs:64",
                                    "rustc_borrowck::nll", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/nll.rs"),
                                    ::tracing_core::__macro_support::Option::Some(64u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::nll"),
                                    ::tracing_core::field::FieldSet::new(&["def"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&def) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let universal_regions = UniversalRegions::new(infcx, def);
            renumber::renumber_mir(infcx, body, promoted);
            if let Some(dumper) = MirDumper::new(infcx.tcx, "renumber", body)
                {
                dumper.dump_mir(body);
            }
            universal_regions
        }
    }
}#[instrument(skip(infcx, body, promoted), level = "debug")]
57pub(crate) fn replace_regions_in_mir<'tcx>(
58    infcx: &BorrowckInferCtxt<'tcx>,
59    body: &mut Body<'tcx>,
60    promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
61) -> UniversalRegions<'tcx> {
62    let def = body.source.def_id().expect_local();
63
64    debug!(?def);
65
66    // Compute named region information. This also renumbers the inputs/outputs.
67    let universal_regions = UniversalRegions::new(infcx, def);
68
69    // Replace all remaining regions with fresh inference variables.
70    renumber::renumber_mir(infcx, body, promoted);
71
72    if let Some(dumper) = MirDumper::new(infcx.tcx, "renumber", body) {
73        dumper.dump_mir(body);
74    }
75
76    universal_regions
77}
78
79/// Computes the closure requirements given the current inference state.
80///
81/// This is intended to be used by before [BorrowCheckRootCtxt::handle_opaque_type_uses]
82/// because applying member constraints may rely on closure requirements.
83/// This is frequently the case of async functions where pretty much everything
84/// happens inside of the inner async block but the opaque only gets constrained
85/// in the parent function.
86pub(crate) fn compute_closure_requirements_modulo_opaques<'tcx>(
87    infcx: &BorrowckInferCtxt<'tcx>,
88    body: &Body<'tcx>,
89    location_map: Rc<DenseLocationMap>,
90    universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
91    constraints: &MirTypeckRegionConstraints<'tcx>,
92) -> Option<ClosureRegionRequirements<'tcx>> {
93    // FIXME(#146079): we shouldn't have to clone all this stuff here.
94    // Computing the region graph should take at least some of it by reference/`Rc`.
95    let lowered_constraints = compute_sccs_applying_placeholder_outlives_constraints(
96        constraints.clone(),
97        &universal_region_relations,
98        infcx,
99    );
100    let mut regioncx = RegionInferenceContext::new(
101        &infcx,
102        lowered_constraints,
103        universal_region_relations.clone(),
104        location_map,
105    );
106
107    let (closure_region_requirements, _nll_errors) = regioncx.solve(infcx, body, None);
108    closure_region_requirements
109}
110
111/// Computes the (non-lexical) regions from the input MIR.
112///
113/// This may result in errors being reported.
114pub(crate) fn compute_regions<'tcx>(
115    root_cx: &mut BorrowCheckRootCtxt<'tcx>,
116    infcx: &BorrowckInferCtxt<'tcx>,
117    body: &Body<'tcx>,
118    location_table: &PoloniusLocationTable,
119    move_data: &MoveData<'tcx>,
120    borrow_set: &BorrowSet<'tcx>,
121    location_map: Rc<DenseLocationMap>,
122    universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
123    constraints: MirTypeckRegionConstraints<'tcx>,
124    mut polonius_facts: Option<AllFacts<RustcFacts>>,
125    mut polonius_context: Option<PoloniusContext>,
126) -> NllOutput<'tcx> {
127    let polonius_output = root_cx.consumer.as_ref().map_or(false, |c| c.polonius_output())
128        || infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();
129
130    let lowered_constraints = compute_sccs_applying_placeholder_outlives_constraints(
131        constraints,
132        &universal_region_relations,
133        infcx,
134    );
135
136    // If requested, emit legacy polonius facts.
137    polonius::legacy::emit_facts(
138        &mut polonius_facts,
139        infcx.tcx,
140        location_table,
141        body,
142        borrow_set,
143        move_data,
144        &universal_region_relations,
145        &lowered_constraints,
146    );
147
148    let mut regioncx = RegionInferenceContext::new(
149        infcx,
150        lowered_constraints,
151        universal_region_relations,
152        location_map,
153    );
154
155    // If requested for `-Zpolonius=next`, convert NLL constraints to localized outlives constraints
156    // and use them to compute loan liveness.
157    if let Some(polonius_context) = polonius_context.as_mut() {
158        polonius_context.compute_loan_liveness(&mut regioncx, body, borrow_set)
159    }
160
161    // If requested: dump NLL facts, and run legacy polonius analysis.
162    let polonius_output = polonius_facts.as_ref().and_then(|polonius_facts| {
163        if infcx.tcx.sess.opts.unstable_opts.nll_facts {
164            let def_id = body.source.def_id();
165            let def_path = infcx.tcx.def_path(def_id);
166            let dir_path = PathBuf::from(&infcx.tcx.sess.opts.unstable_opts.nll_facts_dir)
167                .join(def_path.to_filename_friendly_no_crate());
168            polonius_facts.write_to_dir(dir_path, location_table).unwrap();
169        }
170
171        if polonius_output {
172            let algorithm = infcx.tcx.env_var("POLONIUS_ALGORITHM").unwrap_or("Hybrid");
173            let algorithm = Algorithm::from_str(algorithm).unwrap();
174            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/nll.rs:174",
                        "rustc_borrowck::nll", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/nll.rs"),
                        ::tracing_core::__macro_support::Option::Some(174u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::nll"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("compute_regions: using polonius algorithm {0:?}",
                                                    algorithm) as &dyn Value))])
            });
    } else { ; }
};debug!("compute_regions: using polonius algorithm {:?}", algorithm);
175            let _prof_timer = infcx.tcx.prof.generic_activity("polonius_analysis");
176            Some(Box::new(Output::compute(polonius_facts, algorithm, false)))
177        } else {
178            None
179        }
180    });
181
182    // Solve the region constraints.
183    let (closure_region_requirements, nll_errors) =
184        regioncx.solve(infcx, body, polonius_output.clone());
185
186    NllOutput {
187        regioncx,
188        polonius_input: polonius_facts.map(Box::new),
189        polonius_output,
190        opt_closure_req: closure_region_requirements,
191        nll_errors,
192        polonius_context,
193    }
194}
195
196/// `-Zdump-mir=nll` dumps MIR annotated with NLL specific information:
197/// - free regions
198/// - inferred region values
199/// - region liveness
200/// - inference constraints and their causes
201///
202/// As well as graphviz `.dot` visualizations of:
203/// - the region constraints graph
204/// - the region SCC graph
205pub(super) fn dump_nll_mir<'tcx>(
206    infcx: &BorrowckInferCtxt<'tcx>,
207    body: &Body<'tcx>,
208    regioncx: &RegionInferenceContext<'tcx>,
209    closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
210    borrow_set: &BorrowSet<'tcx>,
211) {
212    let tcx = infcx.tcx;
213    let Some(dumper) = MirDumper::new(tcx, "nll", body) else { return };
214
215    // We want the NLL extra comments printed by default in NLL MIR dumps (they were removed in
216    // #112346). Specifying `-Z mir-include-spans` on the CLI still has priority: for example,
217    // they're always disabled in mir-opt tests to make working with blessed dumps easier.
218    let options = PrettyPrintMirOptions {
219        include_extra_comments: #[allow(non_exhaustive_omitted_patterns)] match infcx.tcx.sess.opts.unstable_opts.mir_include_spans
    {
    MirIncludeSpans::On | MirIncludeSpans::Nll => true,
    _ => false,
}matches!(
220            infcx.tcx.sess.opts.unstable_opts.mir_include_spans,
221            MirIncludeSpans::On | MirIncludeSpans::Nll
222        ),
223    };
224
225    let extra_data = &|pass_where, out: &mut dyn std::io::Write| {
226        emit_nll_mir(tcx, regioncx, closure_region_requirements, borrow_set, pass_where, out)
227    };
228
229    let dumper = dumper.set_extra_data(extra_data).set_options(options);
230
231    dumper.dump_mir(body);
232
233    // Also dump the region constraint graph as a graphviz file.
234    let _ = try {
235        let mut file = dumper.create_dump_file("regioncx.all.dot", body)?;
236        regioncx.dump_graphviz_raw_constraints(tcx, &mut file)?;
237    };
238
239    // Also dump the region constraint SCC graph as a graphviz file.
240    let _ = try {
241        let mut file = dumper.create_dump_file("regioncx.scc.dot", body)?;
242        regioncx.dump_graphviz_scc_constraints(tcx, &mut file)?;
243    };
244}
245
246/// Produces the actual NLL MIR sections to emit during the dumping process.
247pub(crate) fn emit_nll_mir<'tcx>(
248    tcx: TyCtxt<'tcx>,
249    regioncx: &RegionInferenceContext<'tcx>,
250    closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
251    borrow_set: &BorrowSet<'tcx>,
252    pass_where: PassWhere,
253    out: &mut dyn io::Write,
254) -> io::Result<()> {
255    match pass_where {
256        // Before the CFG, dump out the values for each region variable.
257        PassWhere::BeforeCFG => {
258            regioncx.dump_mir(tcx, out)?;
259            out.write_fmt(format_args!("|\n"))writeln!(out, "|")?;
260
261            if let Some(closure_region_requirements) = closure_region_requirements {
262                out.write_fmt(format_args!("| Free Region Constraints\n"))writeln!(out, "| Free Region Constraints")?;
263                for_each_region_constraint(tcx, closure_region_requirements, &mut |msg| {
264                    out.write_fmt(format_args!("| {0}\n", msg))writeln!(out, "| {msg}")
265                })?;
266                out.write_fmt(format_args!("|\n"))writeln!(out, "|")?;
267            }
268
269            if borrow_set.len() > 0 {
270                out.write_fmt(format_args!("| Borrows\n"))writeln!(out, "| Borrows")?;
271                for (borrow_idx, borrow_data) in borrow_set.iter_enumerated() {
272                    out.write_fmt(format_args!("| {0:?}: issued at {1:?} in {2:?}\n", borrow_idx,
        borrow_data.reserve_location, borrow_data.region))writeln!(
273                        out,
274                        "| {:?}: issued at {:?} in {:?}",
275                        borrow_idx, borrow_data.reserve_location, borrow_data.region
276                    )?;
277                }
278                out.write_fmt(format_args!("|\n"))writeln!(out, "|")?;
279            }
280        }
281
282        PassWhere::BeforeLocation(_) => {}
283
284        PassWhere::AfterTerminator(_) => {}
285
286        PassWhere::BeforeBlock(_) | PassWhere::AfterLocation(_) | PassWhere::AfterCFG => {}
287    }
288    Ok(())
289}
290
291pub(super) fn dump_annotation<'tcx, 'infcx>(
292    infcx: &'infcx BorrowckInferCtxt<'tcx>,
293    body: &Body<'tcx>,
294    regioncx: &RegionInferenceContext<'tcx>,
295    closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
296) {
297    let tcx = infcx.tcx;
298    let base_def_id = tcx.typeck_root_def_id(body.source.def_id());
299    if !{
    {
            'done:
                {
                for i in tcx.get_all_attrs(base_def_id) {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::RustcRegions) =>
                            {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(tcx.get_all_attrs(base_def_id), AttributeKind::RustcRegions) {
300        return;
301    }
302
303    // When the enclosing function is tagged with `#[rustc_regions]`,
304    // we dump out various bits of state as warnings. This is useful
305    // for verifying that the compiler is behaving as expected. These
306    // warnings focus on the closure region requirements -- for
307    // viewing the intraprocedural state, the -Zdump-mir output is
308    // better.
309
310    let def_span = tcx.def_span(body.source.def_id());
311    let err = if let Some(closure_region_requirements) = closure_region_requirements {
312        let mut err = infcx.dcx().struct_span_note(def_span, "external requirements");
313
314        regioncx.annotate(tcx, &mut err);
315
316        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("number of external vids: {0}",
                closure_region_requirements.num_external_vids))
    })format!(
317            "number of external vids: {}",
318            closure_region_requirements.num_external_vids
319        ));
320
321        // Dump the region constraints we are imposing *between* those
322        // newly created variables.
323        for_each_region_constraint(tcx, closure_region_requirements, &mut |msg| {
324            err.note(msg);
325            Ok(())
326        })
327        .unwrap();
328
329        err
330    } else {
331        let mut err = infcx.dcx().struct_span_note(def_span, "no external requirements");
332        regioncx.annotate(tcx, &mut err);
333        err
334    };
335
336    // FIXME(@lcnr): We currently don't dump the inferred hidden types here.
337    err.emit();
338}
339
340fn for_each_region_constraint<'tcx>(
341    tcx: TyCtxt<'tcx>,
342    closure_region_requirements: &ClosureRegionRequirements<'tcx>,
343    with_msg: &mut dyn FnMut(String) -> io::Result<()>,
344) -> io::Result<()> {
345    for req in &closure_region_requirements.outlives_requirements {
346        let subject = match req.subject {
347            ClosureOutlivesSubject::Region(subject) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", subject))
    })format!("{subject:?}"),
348            ClosureOutlivesSubject::Ty(ty) => {
349                {
    let _guard = NoTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("{0}",
                    ty.instantiate(tcx, |vid| ty::Region::new_var(tcx, vid))))
        })
}with_no_trimmed_paths!(format!(
350                    "{}",
351                    ty.instantiate(tcx, |vid| ty::Region::new_var(tcx, vid))
352                ))
353            }
354        };
355        with_msg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("where {0}: {1:?}", subject,
                req.outlived_free_region))
    })format!("where {}: {:?}", subject, req.outlived_free_region,))?;
356    }
357    Ok(())
358}
359
360pub(crate) trait ConstraintDescription {
361    fn description(&self) -> &'static str;
362}