Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deref coercions and &-to-* coercions don't compose #32122

Closed
carlpaten opened this issue Mar 8, 2016 · 6 comments · Fixed by #71540
Closed

Deref coercions and &-to-* coercions don't compose #32122

carlpaten opened this issue Mar 8, 2016 · 6 comments · Fixed by #71540
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@carlpaten
Copy link

Minimal repro:

use std::ops::Deref;

struct A (u8);

impl Deref for A {
    type Target = u8;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn main() {
    let a = A(0);

    //compiles
    let b: &u8 = &a;
    let c: *const u8 = b;

    //error
    let d: *const u8 = &a;
}

Compile error:

coerce.rs:15:24: 15:26 error: mismatched types:
 expected `*const u8`,
    found `&A`
(expected u8,
    found struct `A`) [E0308]
coerce.rs:15     let c: *const u8 = &a;
                                    ^~
coerce.rs:15:24: 15:26 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
@carlpaten
Copy link
Author

@bluss thanks for walking me through this.

@bluss
Copy link
Member

bluss commented Mar 8, 2016

RFC 401 says that coercions are transitive, but I also realize that it does not consider deref coercions at all.

@steveklabnik steveklabnik added the A-typesystem Area: The type system label Mar 11, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Jan 25, 2020

While I'm not sure if this should be fixed, we can at least fix the diagnostic to tell to user to use &*a instead of &a.

cc @rust-lang/wg-diagnostics

@oli-obk oli-obk added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Jan 25, 2020
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 25, 2020
@estebank
Copy link
Contributor

This should probably be added to

pub fn check_for_cast(
&self,
err: &mut DiagnosticBuilder<'_>,
expr: &hir::Expr<'_>,
checked_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
) -> bool {

RalfJung added a commit to RalfJung/rust that referenced this issue Apr 30, 2020
Suggest deref when coercing `ty::Ref` to `ty::RawPtr`

Fixes rust-lang#32122

Currently we do autoderef when casting `ty::Ref` ->`ty::Ref`, but we don't autoderef when casting `ty::Ref` -> `ty::RawPtr`. This PR make the compiler suggests deref when coercing `ty::Ref` to `ty::RawPtr`
@bors bors closed this as completed in 58d955e Apr 30, 2020
@eddyb
Copy link
Member

eddyb commented May 1, 2020

cc @nikomatsakis Should we close this issue based on only a diagnostics improvement?

@nikomatsakis
Copy link
Contributor

A good question. I do sort of think that coercions "should" compose, but I also suspect there are complications, and am somewhat tempted to say it's "working well enough" and leave it closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants