rust-lang / rust-lang/rust

`try_as_dyn` does not work with builtin impls

Open
#162,134 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-dyn-trait C-bug F-try_as_dyn T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Lifting this from a discussion on Zulip.

core::any::try_as_dyn doesn't work (always returns None) when trying to access builtin impls in any way:

trait SyncAble {}
impl<T> SyncAble for T where T: Sync /* <- builtin */ {}

println!("{}", core::any::try_as_dyn::<u32, dyn SyncAble>(&0).is_some());

Thats quite annoying because checking whether T: Sync holds would potentially allow for optimizations (offloading computations). The same applies to all other builtin impl's. This can be quite confusing, especially because its not consistent for a single trait either.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with core::any::try_as_dyn and the linked Rust Playground case. Read the linked Zulip discussion for the intended behavior and constraints around builtin impls. The issue is complete when try_as_dyn handles builtin implementations consistently rather than returning None unexpectedly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.