Skip to content

Commit ab63680

Browse files
committed
use the right typing mode in lints
1 parent feee5f6 commit ab63680

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,27 @@ pub struct InferCtxt<'tcx> {
319319
pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
320320
}
321321

322+
impl<'tcx> Drop for InferCtxt<'tcx> {
323+
fn drop(&mut self) {
324+
// Defuse the drop bomb in the OpaqueTypeStorage when we're in TypingMode::Borrowck,
325+
// and the InferCtxt doesn't consider regions. This is okay since in `Borrowck`,
326+
// the only reason we care about opaques is in relation to regions.
327+
// In some places *after* typeck, like in lints we use `TypingMode::Borrowck`
328+
// to prevent defining opaque types and we simply don't care about regions.
329+
match self.typing_mode() {
330+
TypingMode::Coherence
331+
| TypingMode::Analysis { .. }
332+
| TypingMode::PostBorrowckAnalysis { .. }
333+
| TypingMode::PostAnalysis => {}
334+
TypingMode::Borrowck { .. } => {
335+
if !self.considering_regions {
336+
let _ = self.inner.borrow_mut().opaque_type_storage.take_opaque_types();
337+
}
338+
}
339+
}
340+
}
341+
}
342+
322343
/// See the `error_reporting` module for more details.
323344
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
324345
pub enum ValuePairs<'tcx> {

compiler/rustc_lint/src/context.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,14 @@ impl<'tcx> LateContext<'tcx> {
633633
/// The typing mode of the currently visited node. Use this when
634634
/// building a new `InferCtxt`.
635635
pub fn typing_mode(&self) -> TypingMode<'tcx> {
636-
// FIXME(#132279): In case we're in a body, we should use a typing
637-
// mode which reveals the opaque types defined by that body.
638-
TypingMode::non_body_analysis()
636+
if let Some(body_id) = self.enclosing_body
637+
&& self.tcx.use_typing_mode_borrowck()
638+
{
639+
let def_id = self.tcx.hir_enclosing_body_owner(body_id.hir_id);
640+
TypingMode::borrowck(self.tcx, def_id)
641+
} else {
642+
TypingMode::non_body_analysis()
643+
}
639644
}
640645

641646
pub fn typing_env(&self) -> TypingEnv<'tcx> {

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
8484
}
8585

8686
let def_id = opaque.def_id.to_def_id();
87-
let infcx = &cx.tcx.infer_ctxt().build(cx.typing_mode());
87+
let infcx = &cx.tcx.infer_ctxt().ignoring_regions().build(cx.typing_mode());
8888
// For every projection predicate in the opaque type's explicit bounds,
8989
// check that the type that we're assigning actually satisfies the bounds
9090
// of the associated type.

0 commit comments

Comments
 (0)