crown: Check closure bodies for unrooted types and fix root TransmitBodyPromise* (#39534)

We should also check closure bodies for unrooted expressions. 

Testing: New crown tests
Fixes: #37331
Fixes (partial): #37330

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Sam 2025-09-29 10:57:23 +02:00 committed by GitHub
parent 6c062bbea8
commit aaa7f83176
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 7 deletions

View file

@ -234,6 +234,7 @@ impl TransmitBodyConnectHandler {
task!(setup_native_body_promise_handler: move || {
let rooted_stream = stream.root();
let global = rooted_stream.global();
let cx = GlobalScope::get_cx();
// Step 4, the result of reading a chunk from bodys stream with reader.
let promise = rooted_stream.read_a_chunk(CanGc::note());
@ -241,20 +242,20 @@ impl TransmitBodyConnectHandler {
// Step 5, the parallel steps waiting for and handling the result of the read promise,
// are a combination of the promise native handler here,
// and the corresponding IPC route in `component::net::http_loader`.
let promise_handler = Box::new(TransmitBodyPromiseHandler {
rooted!(in(*cx) let mut promise_handler = Some(TransmitBodyPromiseHandler {
bytes_sender: bytes_sender.clone(),
stream: Dom::from_ref(&rooted_stream.clone()),
control_sender: control_sender.clone(),
});
}));
let rejection_handler = Box::new(TransmitBodyPromiseRejectionHandler {
rooted!(in(*cx) let mut rejection_handler = Some(TransmitBodyPromiseRejectionHandler {
bytes_sender,
stream: Dom::from_ref(&rooted_stream.clone()),
control_sender,
});
}));
let handler =
PromiseNativeHandler::new(&global, Some(promise_handler), Some(rejection_handler), CanGc::note());
PromiseNativeHandler::new(&global, promise_handler.take().map(|h| Box::new(h) as Box<_>), rejection_handler.take().map(|h| Box::new(h) as Box<_>), CanGc::note());
let realm = enter_realm(&*global);
let comp = InRealm::Entered(&realm);
@ -278,6 +279,8 @@ struct TransmitBodyPromiseHandler {
control_sender: IpcSender<BodyChunkRequest>,
}
impl js::gc::Rootable for TransmitBodyPromiseHandler {}
impl Callback for TransmitBodyPromiseHandler {
/// Step 5 of <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
fn callback(&self, cx: JSContext, v: HandleValue, _realm: InRealm, can_gc: CanGc) {
@ -332,6 +335,8 @@ struct TransmitBodyPromiseRejectionHandler {
control_sender: IpcSender<BodyChunkRequest>,
}
impl js::gc::Rootable for TransmitBodyPromiseRejectionHandler {}
impl Callback for TransmitBodyPromiseRejectionHandler {
/// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {

View file

@ -399,10 +399,10 @@ impl<'tcx> LateLintPass<'tcx> for UnrootedPass {
n.as_str() == "default" ||
n.as_str() == "Wrap"
},
visit::FnKind::Closure => return,
visit::FnKind::Closure => false,
};
if !in_derive_expn(span) {
if !in_derive_expn(span) && !matches!(kind, visit::FnKind::Closure) {
let sig = cx.tcx.type_of(def_id).skip_binder().fn_sig(cx.tcx);
for (arg, ty) in decl.inputs.iter().zip(sig.inputs().skip_binder().iter()) {

View file

@ -0,0 +1,14 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//@rustc-env:RUSTC_BOOTSTRAP=1
#[crown::unrooted_must_root_lint::must_root]
struct Foo(i32);
fn foo2() {
let foo = Foo(0);
//~^ ERROR: Expression of type Foo must be rooted
}
fn main() {}

View file

@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//@rustc-env:RUSTC_BOOTSTRAP=1
#[crown::unrooted_must_root_lint::must_root]
struct Foo(i32);
fn foo2() {
|| {
let foo = Foo(0);
//~^ ERROR: Expression of type Foo must be rooted
};
}
fn main() {}