Skip to content

Conversation

@sander-machado
Copy link

Rustc 1.86 stabilizes a new features called trait_upcasting. As quoted from the rust docs "Enable upcasts from dyn Trait1 to dyn Trait2 if Trait1 is a subtrait of Trait2". Which is exactly what this unsafe code was doing. I added the new syntax and increased the compiler version to 1.86 to enable this feature and remove these unsafe blocks. There were also multiple places where this check was called without any reason as it's checked at construction (and would just panic either way).

Related to: rust-lang/rust#65991

@sander-machado sander-machado marked this pull request as ready for review July 20, 2025 23:05
@zeenix
Copy link
Contributor

zeenix commented Jul 20, 2025

Cool. I'll look at it in more detail later but in the meantime, please split the commit to keep commits atomic. Here, I see at least 3 logical changes: removal of unneeded checks (which I'm not too sure about), bumping of MSRV and removal of unsafe code.

@zeenix
Copy link
Contributor

zeenix commented Aug 14, 2025

@sander-machado ping?

@zeenix
Copy link
Contributor

zeenix commented Dec 17, 2025

@sander-machado I think Rust 1.86 is not too new anymore. Let's bump the MSRV as part of this PR? You'll want to rebase though.

@sander-machado
Copy link
Author

@sander-machado I think Rust 1.86 is not too new anymore. Let's bump the MSRV as part of this PR? You'll want to rebase though.

Great I'll update my PR once that is done!

@zeenix
Copy link
Contributor

zeenix commented Dec 19, 2025

@sander-machado I think Rust 1.86 is not too new anymore. Let's bump the MSRV as part of this PR? You'll want to rebase though.

Great I'll update my PR once that is done!

I meant, you bump the MSRV as part of this PR but it's about to happen with another PR that's about to be merged: #1620. Please rebase on that.

@sander-machado sander-machado force-pushed the remove-unsafe-upcasting-code branch 2 times, most recently from d643b8f to 638e74f Compare December 19, 2025 18:14
@sander-machado
Copy link
Author

@sander-machado I think Rust 1.86 is not too new anymore. Let's bump the MSRV as part of this PR? You'll want to rebase though.

Great I'll update my PR once that is done!

I meant, you bump the MSRV as part of this PR but it's about to happen with another PR that's about to be merged: #1620. Please rebase on that.

Yeah I was referring to that one (had to bump it 1 more anyway), as that's a coworker who made it. But yeah that's not obvious for you ofcourse haha. I have updated my commits to hopefully be atomic and tried to use to correct emojis. Btw I noticed that if you click the name of the emoji you copy the emoji string which almost made me use that instead before I noticed maybe that's why you keep getting people use the string?

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 20, 2025

CodSpeed Performance Report

Merging #1439 will not alter performance

Comparing barcoopensource:remove-unsafe-upcasting-code (638e74f) with main (8c219a9)

Summary

✅ 22 untouched

Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

LGTM apart from one thing and the usual nitpicks on commit messages: 😄

  • They're missing the zb: prefix.
  • second one could be shorter to be close to the guideline, something likeReplace unsafe downcast API w/ std's.
  • A description would be nice, even if a short one. For the second commit, you could use the same description as the PR's since this is the main commit.
  • typo: build ins -> built-ins. However this becomes irrelevant if you change the title.

Comment on lines +276 to +278
// Ensure what we return can later be dowcasted safely.
let lock: &dyn Any = &*lock.read().await;
let _lock: &I = lock.downcast_ref::<I>().ok_or(Error::InterfaceNotFound)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

the lock isn't the lock but rather the ref. Why is this change needed btw? It looks identical to the one we already have. 🤔

Copy link
Author

Choose a reason for hiding this comment

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

you need to explicitly make the detour with the &dyn Any otherwise the compiler doesn't like it

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok. Would be good to change the variable names still. 😉

Rustc 1.86 stabilizes a new features called trait_upcasting. As quoted from the rust docs "Enable upcasts from dyn Trait1 to dyn Trait2 if Trait1 is a subtrait of Trait2". Which is exactly what this unsafe code was doing. I added the new syntax and increased the compiler version to 1.86 to enable this feature and remove these unsafe blocks. There were also multiple places where this check was called without any reason as it's checked at construction (and would just panic either way).

Related to: rust-lang/rust#65991
@sander-machado sander-machado force-pushed the remove-unsafe-upcasting-code branch from 638e74f to e6b126a Compare December 25, 2025 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants