Skip to content

Do not add implied outlives bounds containing external regions/params only#153027

Draft
ShoyuVanilla wants to merge 2 commits intorust-lang:mainfrom
ShoyuVanilla:external-implied-bounds
Draft

Do not add implied outlives bounds containing external regions/params only#153027
ShoyuVanilla wants to merge 2 commits intorust-lang:mainfrom
ShoyuVanilla:external-implied-bounds

Conversation

@ShoyuVanilla
Copy link
Member

@ShoyuVanilla ShoyuVanilla commented Feb 23, 2026

Fixes #151637

The actuall cause wasn't that the closure's return type is not being wf checked.
For example, the following code is errored out as intended:

struct Wrap<T: Default>(T);

fn error<T>(x: T) {
    || Wrap(x);
    //~^ ERROR: the trait bound `T: Default` is not satisfied
}

The problem was that we are adding implied bound T: 'static even when borrowck-ing the closures.

struct Wrap<T: 'static>(T);

fn no_error<T>(x: T) {
    || Wrap(x);
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 23, 2026
// for it, we might fail with the type test for it, which contains the opaque type
// and therefore `'b` as well. The problem is that if the normalized opaque type doesn't
// mentions `'b`, we have no local free region constrained by it, and end up
// emitting a borrowck error instead of propagating the closure requirements.
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering external bounds here regresses this

.map(|&n| Container::new(n, &resolver))

@ShoyuVanilla
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 23, 2026
Do not add outlives implied bounds that contains external regions/params only
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 23, 2026
@ShoyuVanilla ShoyuVanilla changed the title Do not add outlives implied bounds that contains external regions/params only Do not add outlives implied bounds containing external regions/params only Feb 23, 2026
@ShoyuVanilla ShoyuVanilla changed the title Do not add outlives implied bounds containing external regions/params only Do not add implied outlives bounds containing external regions/params only Feb 23, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 23, 2026

☀️ Try build successful (CI)
Build commit: 484ac15 (484ac156cc381bb2cfc74c800423322f261d074d, parent: eeb94be79adc9df7a09ad0b2421f16e60e6d932c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (484ac15): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (secondary 0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.4% [2.9%, 7.9%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.3% [-4.5%, -4.1%] 2
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 481.498s -> 479.757s (-0.36%)
Artifact size: 395.88 MiB -> 397.89 MiB (0.51%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 24, 2026
@ShoyuVanilla
Copy link
Member Author

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-153027 created and queued.
🤖 Automatically detected try build 484ac15
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 24, 2026
@ShoyuVanilla
Copy link
Member Author

This doesn't feel correct. I think I should rather add implied bounds as before and directly propagate them again as requirements.
@craterbot cancel

@craterbot
Copy link
Collaborator

🗑️ Experiment pr-153027 deleted!

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Feb 24, 2026
@lcnr
Copy link
Contributor

lcnr commented Feb 27, 2026

This doesn't feel correct. I think I should rather add implied bounds as before and directly propagate them again as requirements.
@craterbot cancel

hmm, I do feel like only having implied bounds involving the late-bound things from the closure signature seems correct to me 🤔

@ShoyuVanilla
Copy link
Member Author

ShoyuVanilla commented Feb 28, 2026

hmm, I do feel like only having implied bounds involving the late-bound things from the closure signature seems correct to me 🤔

Yeah, only keeping bounds that involve late-bound params feels right to me, too.

But more precisely, I’m currently leaning toward

I'm a bit more inclined to: adding all the implied bounds as before, but gathering the implied bounds don't involve the late bounds and throwing them as propagated closure requirements to the parent body.

I don't have a solid logical basis for this yet 😅, but:

  1. I tried this approach and it avoids the ugly FIXME with the RegionSubAlias bounds I’m not happy with here: Do not add implied outlives bounds containing external regions/params only #153027 (comment)
  2. I'm not even sure such cases actually exist, but I have a vague (and admittedly ungrounded) worry that dropping “legitimate” implied bounds, i.e., ones that can be proven from the parent body and don't mention late-bound params, could cause unexpected borrowck failures.
    Even if those bounds are provable by the parent, removing them outright means the closure gets borrowck-ed without assumptions that the parent could have supplied.
    Conceptually, the mental model I’m gravitating toward is:
    "Closure: Hey, parent, if you want to define me with this signature, these implied bounds are required. I'll borrowck myself assuming they hold, and you're responsible for proving them."

@ShoyuVanilla
Copy link
Member Author

ShoyuVanilla commented Feb 28, 2026

Added a bit messy experimental PoC commit: 96312b1 😅

@rust-log-analyzer
Copy link
Collaborator

The job pr-check-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
/dev/sda15      105M  6.2M   99M   6% /boot/efi
tmpfs           1.6G   12K  1.6G   1% /run/user/1001
================================================================================

Sufficient disk space available (94917392KB >= 52428800KB). Skipping cleanup.
##[group]Run echo "[CI_PR_NUMBER=$num]"
echo "[CI_PR_NUMBER=$num]"
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
---
[RUSTC-TIMING] hashbrown test:false 1.198
    Checking indexmap v2.13.0
[RUSTC-TIMING] tempfile test:false 0.390
    Checking rustdoc-json-types v0.1.0 (/checkout/src/rustdoc-json-types)
error: internal compiler error: compiler/rustc_borrowck/src/universal_regions.rs:963:36: cannot convert `'^0.Named(DefId(0:2764 ~ askama_parser[74e9]::node::{impl#7}::parse::'_#1))` to a region vid


thread 'rustc' (25145) panicked at compiler/rustc_borrowck/src/universal_regions.rs:963:36:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: <rustc_borrowck::universal_regions::UniversalRegions>::to_region_vid
   7: <core::iter::adapters::map::Map<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, <rustc_borrowck::universal_regions::UniversalRegions>::bound_has_late_bound_region::{closure#0}> as core::iter::traits::iterator::Iterator>::try_fold::<(), <core::iter::adapters::flatten::FlattenCompat<_, _>>::iter_try_fold::flatten<rustc_type_ir::walk::TypeWalker<rustc_middle::ty::context::TyCtxt>, (), core::ops::control_flow::ControlFlow<()>, <core::iter::adapters::flatten::FlattenCompat<_, _> as core::iter::traits::iterator::Iterator>::try_fold::flatten<rustc_type_ir::walk::TypeWalker<rustc_middle::ty::context::TyCtxt>, (), core::ops::control_flow::ControlFlow<()>, core::iter::traits::iterator::Iterator::any::check<rustc_middle::ty::generic_args::GenericArg, <rustc_borrowck::universal_regions::UniversalRegions>::bound_has_late_bound_region::{closure#1}>::{closure#0}>::{closure#0}>::{closure#0}, core::ops::control_flow::ControlFlow<()>>
   8: <rustc_borrowck::universal_regions::UniversalRegions>::bound_has_late_bound_region
   9: alloc::vec::in_place_collect::from_iter_in_place::<core::iter::adapters::filter::Filter<alloc::vec::into_iter::IntoIter<rustc_middle::traits::query::OutlivesBound>, <rustc_borrowck::type_check::free_region_relations::UniversalRegionRelationsBuilder>::add_implied_bounds::{closure#1}>, rustc_middle::traits::query::OutlivesBound>
  10: <rustc_borrowck::type_check::free_region_relations::UniversalRegionRelationsBuilder>::add_implied_bounds
  11: rustc_borrowck::type_check::free_region_relations::create
  12: rustc_borrowck::type_check::type_check
  13: rustc_borrowck::borrowck_collect_region_constraints
  14: <rustc_borrowck::root_cx::BorrowCheckRootCtxt>::do_mir_borrowck
  15: rustc_borrowck::mir_borrowck
      [... omitted 1 frame ...]
  16: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  17: rustc_data_structures::sync::parallel::par_for_each_in::<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners<rustc_interface::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}>
  18: <rustc_session::session::Session>::time::<(), rustc_interface::passes::run_required_analyses::{closure#1}>
  19: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  20: <std::thread::local::LocalKey<core::cell::Cell<*const ()>>>::with::<rustc_middle::ty::context::tls::enter_context<<rustc_middle::ty::context::GlobalCtxt>::enter<rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}, core::option::Option<rustc_interface::queries::Linker>>::{closure#1}, core::option::Option<rustc_interface::queries::Linker>>::{closure#0}, core::option::Option<rustc_interface::queries::Linker>>
  21: <rustc_middle::ty::context::TyCtxt>::create_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}>
  22: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  23: rustc_span::create_session_globals_then::<(), rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/askama_parser-0.15.4/rustc-ice-2026-02-28T15_36_05-25143.txt` to your bug report

note: rustc 1.96.0-nightly (15c0391b1 2026-02-28) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -Z binary-dep-depinfo -Z on-broken-pipe=kill

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_borrowck] borrow-checking `node::<impl at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/askama_parser-0.15.4/src/node.rs:662:1: 662:27>::parse`
#1 [analysis] running analysis passes on crate `askama_parser`
end of query stack
[RUSTC-TIMING] indexmap test:false 1.577
    Checking threadpool v1.8.1
[RUSTC-TIMING] threadpool test:false 0.150
[RUSTC-TIMING] askama_parser test:false 4.875
error: could not compile `askama_parser` (lib)

Caused by:
  process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc /checkout/obj/build/bootstrap/debug/rustc --crate-name askama_parser --edition=2024 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/askama_parser-0.15.4/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no --cfg 'feature="config"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("config"))' -C metadata=5a7ddf0a470c5676 -C extra-filename=-9e9dec2128acb182 --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps --extern rustc_hash=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/librustc_hash-50e3dd052137b87b.rmeta --extern serde=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libserde-ccda276fc349560f.rmeta --extern serde_derive=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libserde_derive-8211bf6b587d1a63.so --extern unicode_ident=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libunicode_ident-560cac7982a44d61.rmeta --extern winnow=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libwinnow-6c213ca94942ddde.rmeta --cap-lints allow -Z binary-dep-depinfo` (exit status: 101)
warning: build failed, waiting for other jobs to finish...
[RUSTC-TIMING] itertools test:false 2.983
[RUSTC-TIMING] serde_json test:false 1.140
[RUSTC-TIMING] rustdoc_json_types test:false 2.838
Bootstrap failed while executing `check --target=i686-pc-windows-gnu --host=i686-pc-windows-gnu`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsoundness due to closure return value not being checked for WF

6 participants