Skip to content

Conversation

@ada4a
Copy link
Contributor

@ada4a ada4a commented Oct 22, 2025

v2 of #15934

changelog: [match_as_ref]: also suggest downcasting references

r? @samueltardieu

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref-v2 branch 2 times, most recently from 9cde98a to 7a6f20a Compare October 22, 2025 10:03
}

fn issue15932() {
let _: Option<&i32> = Some(0).as_mut().map(|x| x as _);
Copy link
Member

Choose a reason for hiding this comment

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

Why not Some(0).as_ref()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be clear, I'm not opposed to that solution (though I did end up liking this one more) -- I explicitly opened two PRs so that you could compare the outputs and see which one you prefer

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that we not clear to me, I get a lot of notifications from GitHub.

fn issue15932() {
let _: Option<&i32> = Some(0).as_mut().map(|x| x as _);

let _: Option<&dyn std::fmt::Debug> = Some(0).as_mut().map(|x| x as &mut _ as &_);
Copy link
Member

Choose a reason for hiding this comment

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

Why is the double cast necessary here, or even useful? Some(0).as_mut().map(|x| x as &_) would work, and Some(0).as_ref().map(|x| x as &_) would be even better. Why not even Some(0).as_ref().map(|x| x as _)?

Copy link
Contributor Author

@ada4a ada4a Oct 22, 2025

Choose a reason for hiding this comment

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

To the double-cast part: it's because when I first tried that I got a type ambiguity error. Though now that I think about it, the error was actually caused by me trying to cast &mut u32 as &i32 (note that the types change as well). Fixed that.

As to why I don't use as_ref, see the reply above.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref-v2 branch from 7a6f20a to 789d36f Compare October 22, 2025 10:34
@samueltardieu
Copy link
Member

#15934 will be merged

@ada4a ada4a deleted the 15932-match_as_ref-v2 branch October 22, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants