Make ast types covariant over the allocator lifetime.#2943
Make ast types covariant over the allocator lifetime.#2943Boshen merged 9 commits intooxc-project:mainfrom
Conversation
CodSpeed Performance ReportMerging #2943 will not alter performanceComparing Summary
|
2840793 to
7f49b6a
Compare
|
Are you working on something that requires I'm asking because I don't understand this topic, and need to know more about the context of this change. |
@Boshen Thank you for your time reviewing. I was working on a side project using oxc and bumped1 into this restriction. I just think Also on other day I saw rolldown is also creating self-referencing ast types, so maybe this can make their life a little easier as well. I am no expert to unsafe rust. But seeing both std's and allocator_api2's Footnotes
|
|
Thank you for working on this. Merging because tests, rustc and miri all passed. |
|
Example. fn break_it<'a, 'b: 'a>(a: &mut &'b Vec<i32>, b: &'a Vec<i32>) {
let coerced_a: &mut &'a Vec<i32> = a; // Would be valid if `&mut T` covariant over `T`.
*coerced_a = b;
}
let x = vec![1, 2, 3];
let mut y: &Vec<i32> = &x;
break_it(&mut y, &vec![]);
println!("{y:?}"); // use after free 💥This is not an issue with fn break_it<'a, 'b: 'a>(a: Box<&'b Vec<i32>>, b: &'a Vec<i32>) {
let coerced_a: Box<&'a Vec<i32>> = a; // Would be valid if `&mut T` covariant over `T`.
*coerced_a = b;
}
let x = vec![1, 2, 3];
let mut y: &Vec<i32> = &x;
break_it(Box::new(y), &vec![]);
println!("{y:?}"); // Doesn't compile, `y` is no more.I'd like to replace |
|
Weird, this is causing a stack over only in the code coverage run https://github.com/oxc-project/oxc/actions/runs/8660479790/job/23749372302 |
For the record, this PR increased the stack size just over the edge. A The underlying cause is |
| /// This is used for over coming self-referential structs. | ||
| /// It is a memory leak if the boxed value has a `Drop` implementation. | ||
| pub struct Box<'alloc, T: ?Sized>(pub &'alloc mut T); | ||
| pub struct Box<'alloc, T: ?Sized>(NonNull<T>, PhantomData<&'alloc T>); |
There was a problem hiding this comment.
It should be PhantomData<(T, &'alloc ())> or something like this.
It should convey that Box owns T
[](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [oxlint](https://oxc-project.github.io) ([source](https://togithub.com/oxc-project/oxc/tree/HEAD/npm/oxlint)) | [`0.2.17` -> `0.3.0`](https://renovatebot.com/diffs/npm/oxlint/0.2.17/0.3.0) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>oxc-project/oxc (oxlint)</summary> ### [`v0.3.0`](https://togithub.com/oxc-project/oxc/releases/tag/oxlint_v0.3.0): oxlint v0.3.0 [Compare Source](https://togithub.com/oxc-project/oxc/compare/04f5fc018650d9a5dc6a4b1b40dea941fa07781e...b29aabd6f1a2e9e8cfa7db25c371ab40f79d02a5) #### What's Changed ##### Potential breaking change - `-D all` no longer enables nursery rules, use `-D all -D nursery` instead ##### Features - support eslint globals by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3038](https://togithub.com/oxc-project/oxc/pull/3038) - change no-empty-static-block to correctness - implement `--format checkstyle` by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3044](https://togithub.com/oxc-project/oxc/pull/3044) - implement `--format unix` by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3039](https://togithub.com/oxc-project/oxc/pull/3039) - implement fixer for `typescript-eslint/consistent-type-definitions` by [@​todor-a](https://togithub.com/todor-a) in [https://github.com/oxc-project/oxc/pull/3045](https://togithub.com/oxc-project/oxc/pull/3045) ##### Fixes - fix crashing with `unwrap` in import/no-cycle by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3035](https://togithub.com/oxc-project/oxc/pull/3035) **Full Changelog**: oxc-project/oxc@oxlint_v0.2.18...oxlint_v0.3.0 ### [`v0.2.18`](https://togithub.com/oxc-project/oxc/releases/tag/oxlint_v0.2.18): oxlint v0.2.18 [Compare Source](https://togithub.com/oxc-project/oxc/compare/df11d10a2220e9aa7a33d9ab39ed662c2ba6fdb5...04f5fc018650d9a5dc6a4b1b40dea941fa07781e) #### What's Changed ##### New Rules - correctness: eslint-plugin-unicorn no await in promise methods by [@​camc314](https://togithub.com/camc314) in [https://github.com/oxc-project/oxc/pull/2963](https://togithub.com/oxc-project/oxc/pull/2963) - correctness: eslint-plugin-unicorn no single promise in promise methods by [@​camc314](https://togithub.com/camc314) in [https://github.com/oxc-project/oxc/pull/2962](https://togithub.com/oxc-project/oxc/pull/2962) ##### Features - Add --jsdoc-plugin flag by [@​leaysgur](https://togithub.com/leaysgur) in [https://github.com/oxc-project/oxc/pull/2935](https://togithub.com/oxc-project/oxc/pull/2935) - Implement plugin-jsdoc/check-property-names by [@​leaysgur](https://togithub.com/leaysgur) in [https://github.com/oxc-project/oxc/pull/2989](https://togithub.com/oxc-project/oxc/pull/2989) - eslint/max-len by [@​woai3c](https://togithub.com/woai3c) in [https://github.com/oxc-project/oxc/pull/2874](https://togithub.com/oxc-project/oxc/pull/2874) - remove import/no-unresolved by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3023](https://togithub.com/oxc-project/oxc/pull/3023) - support `oxlint-disable` alongside `eslint-disable` by [@​Boshen](https://togithub.com/Boshen) in [https://github.com/oxc-project/oxc/pull/3024](https://togithub.com/oxc-project/oxc/pull/3024) - jsdoc: Implement require-property rule by [@​leaysgur](https://togithub.com/leaysgur) in [https://github.com/oxc-project/oxc/pull/3011](https://togithub.com/oxc-project/oxc/pull/3011) - jsdoc: Implement require-property-(type|name|description) rules by [@​leaysgur](https://togithub.com/leaysgur) in [https://github.com/oxc-project/oxc/pull/3013](https://togithub.com/oxc-project/oxc/pull/3013) #### New Contributors - [@​branchseer](https://togithub.com/branchseer) made their first contribution in [https://github.com/oxc-project/oxc/pull/2943](https://togithub.com/oxc-project/oxc/pull/2943) - [@​woai3c](https://togithub.com/woai3c) made their first contribution in [https://github.com/oxc-project/oxc/pull/2874](https://togithub.com/oxc-project/oxc/pull/2874) **Full Changelog**: oxc-project/oxc@oxlint_v0.2.17...oxlint_v0.2.18 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMDEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjMxMy4xIiwidGFyZ2V0QnJhbmNoIjoiY2FuYXJ5IiwibGFiZWxzIjpbImRlcGVuZGVuY2llcyJdfQ==-->
|
@branchseer Sorry to resurrect a long-dead issue, but can you please help me with something? What is it that makes But in our case, the https://docs.rs/allocator-api2/0.2.21/src/allocator_api2/stable/raw_vec.rs.html#116-120 And weirdly, neither of them includes a Are you able to shed any light? |
In branchseer@8bebc89 I swapped
|
OK great. Thanks for investigating, and resolving the mystery. Maybe you were right at the time and
Ah ha. Yes, of course. I did not spot the difference |
Why
Due to the usage of
&'alloc mut Tinoxc_allocator::Box, andbumpalo::collections::Vecinoxc_allocator::Vec, ast types are currently invariant over their allocator lifetime'a. This preventsouroborosfrom generatingborrow_*on ast type fields, leading to the unfriendlywith_*api:oxc/crates/oxc_parser/examples/multi-thread.rs
Lines 82 to 84 in c250b28
How
oxc_allocator::Vec, switch toallocator_api2::vec::Vec, which has a covariant relationship with the allocator lifetime.oxc_allocator::Box, usestd::ptr::NonNullwhich is specifically designed to be covariant. I don't useallocator_api2::boxed::Boxbecause it holds the allocator for dropping, so the size is bigger.Downside
Now that
oxc_allocator::Boxuses the unsafeNonNull. It has to be a private field to be safe. This make it impossible to doBox(....)pattern matching.