Skip to content

Commit a6b2a40

Browse files
committedJun 30, 2023
Trying to implement a "borrowck" pass
By overriding the `mir_borrowck` query
1 parent d59363a commit a6b2a40

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed
 

‎compiler/rustc_borrowck/src/consumers.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::traits::DefiningAnchor;
1010
use rustc_middle::ty::TyCtxt;
1111
use std::rc::Rc;
1212

13-
use crate::borrow_set::BorrowSet;
13+
use crate::{borrow_set::BorrowSet, BorrowCheckResult};
1414

1515
pub use super::{
1616
constraints::OutlivesConstraint,
@@ -107,9 +107,18 @@ pub fn get_body_with_borrowck_facts(
107107
def: LocalDefId,
108108
options: ConsumerOptions,
109109
) -> BodyWithBorrowckFacts<'_> {
110+
*do_mir_borrowck(tcx, def, options).1.unwrap()
111+
}
112+
113+
/// Like [`get_body_with_borrowck_facts`], but also return the borrow check results.
114+
pub fn do_mir_borrowck<'tcx>(
115+
tcx: TyCtxt<'tcx>,
116+
def: LocalDefId,
117+
options: ConsumerOptions,
118+
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
110119
let (input_body, promoted) = tcx.mir_promoted(def);
111120
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def)).build();
112121
let input_body: &Body<'_> = &input_body.borrow();
113122
let promoted: &IndexSlice<_, _> = &promoted.borrow();
114-
*super::do_mir_borrowck(&infcx, input_body, promoted, Some(options)).1.unwrap()
123+
super::do_mir_borrowck(&infcx, input_body, promoted, Some(options))
115124
}

‎compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
13661366

13671367
// Evaluate whether `sup_region: sub_region`.
13681368
#[instrument(skip(self), level = "debug", ret)]
1369-
fn eval_outlives(&self, sup_region: RegionVid, sub_region: RegionVid) -> bool {
1369+
pub fn eval_outlives(&self, sup_region: RegionVid, sub_region: RegionVid) -> bool {
13701370
debug!(
13711371
"sup_region's value = {:?} universal={:?}",
13721372
self.region_value_str(sup_region),

‎compiler/rustc_lint/src/context.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,19 @@ impl LintStore {
190190
pub fn register_late_pass(
191191
&mut self,
192192
pass: impl for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx>
193-
+ 'static
194-
+ sync::DynSend
195-
+ sync::DynSync,
193+
+ 'static
194+
+ sync::DynSend
195+
+ sync::DynSync,
196196
) {
197197
self.late_passes.push(Box::new(pass));
198198
}
199199

200200
pub fn register_late_mod_pass(
201201
&mut self,
202202
pass: impl for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx>
203-
+ 'static
204-
+ sync::DynSend
205-
+ sync::DynSync,
203+
+ 'static
204+
+ sync::DynSend
205+
+ sync::DynSync,
206206
) {
207207
self.late_module_passes.push(Box::new(pass));
208208
}
@@ -533,6 +533,7 @@ impl LintStore {
533533
}
534534

535535
/// Context for lint checking outside of type inference.
536+
#[derive(Clone)]
536537
pub struct LateContext<'tcx> {
537538
/// Type context we're checking in.
538539
pub tcx: TyCtxt<'tcx>,
@@ -1145,6 +1146,20 @@ impl LintContext for EarlyContext<'_> {
11451146
}
11461147

11471148
impl<'tcx> LateContext<'tcx> {
1149+
pub fn new(tcx: TyCtxt<'tcx>, enclosing_body: Option<hir::BodyId>) -> Self {
1150+
Self {
1151+
tcx,
1152+
enclosing_body,
1153+
cached_typeck_results: Cell::new(None),
1154+
param_env: ty::ParamEnv::empty(),
1155+
effective_visibilities: &tcx.effective_visibilities(()),
1156+
lint_store: crate::unerased_lint_store(tcx),
1157+
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
1158+
generics: None,
1159+
only_module: false,
1160+
}
1161+
}
1162+
11481163
/// Gets the type-checking results for the current body,
11491164
/// or `None` if outside a body.
11501165
pub fn maybe_typeck_results(&self) -> Option<&'tcx ty::TypeckResults<'tcx>> {

‎compiler/rustc_lint/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
4343
/// Implements the AST traversal for late lint passes. `T` provides the
4444
/// `check_*` methods.
4545
pub struct LateContextAndPass<'tcx, T: LateLintPass<'tcx>> {
46-
context: LateContext<'tcx>,
47-
pass: T,
46+
pub context: LateContext<'tcx>,
47+
pub pass: T,
4848
}
4949

5050
impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {

‎compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub use builtin::SoftLints;
126126
pub use context::{CheckLintNameResult, FindLintError, LintStore};
127127
pub use context::{EarlyContext, LateContext, LintContext};
128128
pub use early::{check_ast_node, EarlyCheckNode};
129-
pub use late::{check_crate, unerased_lint_store};
129+
pub use late::{check_crate, unerased_lint_store, LateContextAndPass};
130130
pub use passes::{EarlyLintPass, LateLintPass};
131131
pub use rustc_session::lint::Level::{self, *};
132132
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};

0 commit comments

Comments
 (0)