diff --git a/compiler/rustc_borrowck/src/type_check/liveness/mod.rs b/compiler/rustc_borrowck/src/type_check/liveness/mod.rs index ac8670a513820..d5c401ae1c6ed 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/mod.rs @@ -1,11 +1,11 @@ +use itertools::{Either, Itertools}; use rustc_data_structures::fx::FxHashSet; use rustc_middle::mir::{Body, Local}; use rustc_middle::ty::{RegionVid, TyCtxt}; -use std::rc::Rc; - use rustc_mir_dataflow::impls::MaybeInitializedPlaces; use rustc_mir_dataflow::move_paths::MoveData; use rustc_mir_dataflow::ResultsCursor; +use std::rc::Rc; use crate::{ constraints::OutlivesConstraintSet, @@ -46,7 +46,8 @@ pub(super) fn generate<'mir, 'tcx>( &typeck.borrowck_context.universal_regions, &typeck.borrowck_context.constraints.outlives_constraints, ); - let live_locals = compute_live_locals(typeck.tcx(), &free_regions, &body); + let (relevant_live_locals, boring_locals) = + compute_relevant_live_locals(typeck.tcx(), &free_regions, &body); let facts_enabled = use_polonius || AllFacts::enabled(typeck.tcx()); let polonius_drop_used = if facts_enabled { @@ -57,48 +58,44 @@ pub(super) fn generate<'mir, 'tcx>( None }; - if !live_locals.is_empty() || facts_enabled { - trace::trace( - typeck, - body, - elements, - flow_inits, - move_data, - live_locals, - polonius_drop_used, - ); - } + trace::trace( + typeck, + body, + elements, + flow_inits, + move_data, + relevant_live_locals, + boring_locals, + polonius_drop_used, + ); } -// The purpose of `compute_live_locals` is to define the subset of `Local` +// The purpose of `compute_relevant_live_locals` is to define the subset of `Local` // variables for which we need to do a liveness computation. We only need // to compute whether a variable `X` is live if that variable contains // some region `R` in its type where `R` is not known to outlive a free // region (i.e., where `R` may be valid for just a subset of the fn body). -fn compute_live_locals<'tcx>( +fn compute_relevant_live_locals<'tcx>( tcx: TyCtxt<'tcx>, free_regions: &FxHashSet, body: &Body<'tcx>, -) -> Vec { - let live_locals: Vec = body - .local_decls - .iter_enumerated() - .filter_map(|(local, local_decl)| { +) -> (Vec, Vec) { + let (boring_locals, relevant_live_locals): (Vec<_>, Vec<_>) = + body.local_decls.iter_enumerated().partition_map(|(local, local_decl)| { if tcx.all_free_regions_meet(&local_decl.ty, |r| { free_regions.contains(&r.to_region_vid()) }) { - None + Either::Left(local) } else { - Some(local) + Either::Right(local) } - }) - .collect(); + }); debug!("{} total variables", body.local_decls.len()); - debug!("{} variables need liveness", live_locals.len()); + debug!("{} variables need liveness", relevant_live_locals.len()); debug!("{} regions outlive free regions", free_regions.len()); - live_locals + (relevant_live_locals, boring_locals) } /// Computes all regions that are (currently) known to outlive free diff --git a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs index 169de23facce2..a69eec47c7bcc 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs @@ -41,12 +41,13 @@ pub(super) fn trace<'mir, 'tcx>( elements: &Rc, flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>, move_data: &MoveData<'tcx>, - live_locals: Vec, + relevant_live_locals: Vec, + boring_locals: Vec, polonius_drop_used: Option>, ) { debug!("trace()"); - let local_use_map = &LocalUseMap::build(&live_locals, elements, body); + let local_use_map = &LocalUseMap::build(&relevant_live_locals, elements, body); let cx = LivenessContext { typeck, @@ -61,10 +62,12 @@ pub(super) fn trace<'mir, 'tcx>( let mut results = LivenessResults::new(cx); if let Some(drop_used) = polonius_drop_used { - results.add_extra_drop_facts(drop_used, live_locals.iter().copied().collect()) + results.add_extra_drop_facts(drop_used, relevant_live_locals.iter().copied().collect()) } - results.compute_for_all_locals(live_locals); + results.compute_for_all_locals(relevant_live_locals); + + results.dropck_boring_locals(boring_locals); } /// Contextual state for the type-liveness generator. @@ -133,8 +136,8 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> { } } - fn compute_for_all_locals(&mut self, live_locals: Vec) { - for local in live_locals { + fn compute_for_all_locals(&mut self, relevant_live_locals: Vec) { + for local in relevant_live_locals { self.reset_local_state(); self.add_defs_for(local); self.compute_use_live_points_for(local); @@ -157,6 +160,24 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> { } } + // Runs dropck for locals whose liveness isn't relevant. This is + // necessary to eagerly detect unbound recursion during drop glue computation. + fn dropck_boring_locals(&mut self, boring_locals: Vec) { + for local in boring_locals { + let local_ty = self.cx.body.local_decls[local].ty; + let drop_data = self.cx.drop_data.entry(local_ty).or_insert_with({ + let typeck = &mut self.cx.typeck; + move || LivenessContext::compute_drop_data(typeck, local_ty) + }); + + drop_data.dropck_result.report_overflows( + self.cx.typeck.infcx.tcx, + self.cx.body.local_decls[local].source_info.span, + local_ty, + ); + } + } + /// Add extra drop facts needed for Polonius. /// /// Add facts for all locals with free regions, since regions may outlive @@ -164,12 +185,12 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> { fn add_extra_drop_facts( &mut self, drop_used: Vec<(Local, Location)>, - live_locals: FxHashSet, + relevant_live_locals: FxHashSet, ) { let locations = IntervalSet::new(self.cx.elements.num_points()); for (local, location) in drop_used { - if !live_locals.contains(&local) { + if !relevant_live_locals.contains(&local) { let local_ty = self.cx.body.local_decls[local].ty; if local_ty.has_free_regions() { self.cx.add_drop_live_facts_for(local, local_ty, &[location], &locations); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 294f56d16b10f..b8bb93891c27f 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1825,7 +1825,8 @@ rustc_queries! { remap_env_constness } - /// Do not call this query directly: invoke `infcx.at().dropck_outlives()` instead. + /// Do not call this query directly: + /// invoke `DropckOutlives::new(dropped_ty)).fully_perform(typeck.infcx)` instead. query dropck_outlives( goal: CanonicalTyGoal<'tcx> ) -> Result< diff --git a/compiler/rustc_typeck/src/check/dropck.rs b/compiler/rustc_typeck/src/check/dropck.rs index ed3b9f2db1f8e..a4013e10525ee 100644 --- a/compiler/rustc_typeck/src/check/dropck.rs +++ b/compiler/rustc_typeck/src/check/dropck.rs @@ -8,8 +8,6 @@ use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::util::IgnoreRegions; use rustc_middle::ty::{self, Predicate, Ty, TyCtxt}; use rustc_span::Span; -use rustc_trait_selection::traits::query::dropck_outlives::AtExt; -use rustc_trait_selection::traits::ObligationCause; /// This function confirms that the `Drop` implementation identified by /// `drop_impl_did` is not any more specialized than the type it is @@ -234,18 +232,14 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( /// This function is not only checking that the dropck obligations are met for /// the given type, but it's also currently preventing non-regular recursion in /// types from causing stack overflows (dropck_no_diverge_on_nonregular_*.rs). +/// +/// FIXME: Completely rip out dropck and regionck. pub(crate) fn check_drop_obligations<'a, 'tcx>( - rcx: &mut RegionCtxt<'a, 'tcx>, - ty: Ty<'tcx>, - span: Span, - body_id: hir::HirId, + _rcx: &mut RegionCtxt<'a, 'tcx>, + _ty: Ty<'tcx>, + _span: Span, + _body_id: hir::HirId, ) { - debug!("check_drop_obligations typ: {:?}", ty); - - let cause = &ObligationCause::misc(span, body_id); - let infer_ok = rcx.infcx.at(cause, rcx.fcx.param_env).dropck_outlives(ty); - debug!("dropck_outlives = {:#?}", infer_ok); - rcx.fcx.register_infer_ok_obligations(infer_ok); } // This is an implementation of the TypeRelation trait with the diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.rs b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.rs index 6ca5f5e3c5fcf..43c1c7759789d 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.rs +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.rs @@ -23,5 +23,4 @@ enum FingerTree { fn main() { let ft = //~ ERROR overflow while adding drop-check rules for FingerTree FingerTree::Single(1); - //~^ ERROR overflow while adding drop-check rules for FingerTree } diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr index 5df69e4649df5..c447e2f7987cc 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr @@ -6,13 +6,5 @@ LL | let ft = | = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -error[E0320]: overflow while adding drop-check rules for FingerTree - --> $DIR/dropck_no_diverge_on_nonregular_1.rs:25:9 - | -LL | FingerTree::Single(1); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - -error: aborting due to 2 previous errors +error: aborting due to previous error diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.rs b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.rs index d34f7e326d15f..edd07652e53f7 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.rs +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.rs @@ -22,5 +22,4 @@ enum FingerTree { fn main() { let ft = //~ ERROR overflow while adding drop-check rules for FingerTree FingerTree::Single(1); - //~^ ERROR overflow while adding drop-check rules for FingerTree } diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr index d34097d401004..cd4706dd903f4 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr @@ -6,13 +6,5 @@ LL | let ft = | = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -error[E0320]: overflow while adding drop-check rules for FingerTree - --> $DIR/dropck_no_diverge_on_nonregular_2.rs:24:9 - | -LL | FingerTree::Single(1); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - -error: aborting due to 2 previous errors +error: aborting due to previous error diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.rs b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.rs index 558b4342da74d..af7402ca4a1ce 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.rs +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.rs @@ -31,6 +31,5 @@ enum Wrapper { fn main() { let w = //~ ERROR overflow while adding drop-check rules for Option Some(Wrapper::Simple::); - //~^ ERROR overflow while adding drop-check rules for Option - //~| ERROR overflow while adding drop-check rules for Wrapper + //~^ ERROR overflow while adding drop-check rules for Wrapper } diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr index b24e1d1bf7927..18cd1b6cd413b 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr @@ -6,14 +6,6 @@ LL | let w = | = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -error[E0320]: overflow while adding drop-check rules for Option> - --> $DIR/dropck_no_diverge_on_nonregular_3.rs:33:9 - | -LL | Some(Wrapper::Simple::); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - error[E0320]: overflow while adding drop-check rules for Wrapper --> $DIR/dropck_no_diverge_on_nonregular_3.rs:33:14 | @@ -22,5 +14,5 @@ LL | Some(Wrapper::Simple::); | = note: overflowed on FingerTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors diff --git a/src/test/ui/infinite/infinite-struct.rs b/src/test/ui/infinite/infinite-struct.rs index 70a203ea6e814..74185dc597b52 100644 --- a/src/test/ui/infinite/infinite-struct.rs +++ b/src/test/ui/infinite/infinite-struct.rs @@ -1,6 +1,5 @@ struct Take(Take); //~^ ERROR has infinite size -//~| ERROR cycle detected // check that we don't hang trying to find the tail of a recursive struct (#79437) fn foo() -> Take { diff --git a/src/test/ui/infinite/infinite-struct.stderr b/src/test/ui/infinite/infinite-struct.stderr index 214be091ccece..5a6d13786d197 100644 --- a/src/test/ui/infinite/infinite-struct.stderr +++ b/src/test/ui/infinite/infinite-struct.stderr @@ -11,16 +11,6 @@ help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Take` repre LL | struct Take(Box); | ++++ + -error[E0391]: cycle detected when computing drop-check constraints for `Take` - --> $DIR/infinite-struct.rs:1:1 - | -LL | struct Take(Take); - | ^^^^^^^^^^^ - | - = note: ...which immediately requires computing drop-check constraints for `Take` again - = note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, constness: NotConst }, value: Take } }` - -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0072, E0391. -For more information about an error, try `rustc --explain E0072`. +For more information about this error, try `rustc --explain E0072`. diff --git a/src/test/ui/infinite/infinite-tag-type-recursion.rs b/src/test/ui/infinite/infinite-tag-type-recursion.rs index 8578c5545bc90..87a9e08dd381a 100644 --- a/src/test/ui/infinite/infinite-tag-type-recursion.rs +++ b/src/test/ui/infinite/infinite-tag-type-recursion.rs @@ -1,5 +1,4 @@ enum MList { Cons(isize, MList), Nil } //~^ ERROR recursive type `MList` has infinite size -//~| ERROR cycle detected when computing drop-check constraints for `MList` fn main() { let a = MList::Cons(10, MList::Cons(11, MList::Nil)); } diff --git a/src/test/ui/infinite/infinite-tag-type-recursion.stderr b/src/test/ui/infinite/infinite-tag-type-recursion.stderr index 1802c7599a3b5..d2dad4b9178d9 100644 --- a/src/test/ui/infinite/infinite-tag-type-recursion.stderr +++ b/src/test/ui/infinite/infinite-tag-type-recursion.stderr @@ -11,16 +11,6 @@ help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `MList` repr LL | enum MList { Cons(isize, Box), Nil } | ++++ + -error[E0391]: cycle detected when computing drop-check constraints for `MList` - --> $DIR/infinite-tag-type-recursion.rs:1:1 - | -LL | enum MList { Cons(isize, MList), Nil } - | ^^^^^^^^^^ - | - = note: ...which immediately requires computing drop-check constraints for `MList` again - = note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, constness: NotConst }, value: MList } }` - -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0072, E0391. -For more information about an error, try `rustc --explain E0072`. +For more information about this error, try `rustc --explain E0072`. diff --git a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs index c857eb459b8de..c9e93174e20fe 100644 --- a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs +++ b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs @@ -1,18 +1,14 @@ -// Dropck shouldn't hit a recursion limit from checking `S` since it has -// no free regions or type parameters. -// Codegen however, has to error for the infinitely many `drop_in_place` -// functions it has been asked to create. - -// build-fail -// normalize-stderr-test: ".nll/" -> "/" -// compile-flags: -Zmir-opt-level=0 +// `S` is infinitely recursing so it's not possible to generate a finite +// drop impl (ignoring polymorphization). +// +// Dropck should therefore detect that this is the case and eagerly error. struct S { t: T, s: Box>, } -fn f(x: S) {} +fn f(x: S) {} //~ ERROR overflow while adding drop-check rules for S fn main() { // Force instantiation. diff --git a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr index d749ee00c22bf..1da29be43db86 100644 --- a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr +++ b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr @@ -1,15 +1,10 @@ -error: reached the recursion limit while instantiating `std::ptr::drop_in_place::))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +error[E0320]: overflow while adding drop-check rules for S + --> $DIR/issue-38591-non-regular-dropck-recursion.rs:11:6 | -LL | pub unsafe fn drop_in_place(to_drop: *mut T) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | fn f(x: S) {} + | ^ | -note: `std::ptr::drop_in_place` defined here - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL - | -LL | pub unsafe fn drop_in_place(to_drop: *mut T) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: the full type name has been written to '$TEST_BUILD_DIR/recursion/issue-38591-non-regular-dropck-recursion/issue-38591-non-regular-dropck-recursion.long-type.txt' + = note: overflowed on S error: aborting due to previous error