Skip to content

Impling a trait on T fails to make it accessable for generic types. #6898

New issue

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

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

Already on GitHub? # to your account

Closed
brendanzab opened this issue Jun 2, 2013 · 3 comments
Closed
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@brendanzab
Copy link
Member

Consider the following code:

use std::unstable::intrinsics;

/// Returns the size of a type
pub fn size_of<T>() -> uint {
    TypeInfo::size_of::<T>()
}

/// Returns the size of the type that `val` points to
pub fn size_of_val<T>(val: &T) -> uint {
    val.size_of_val()
}

pub trait TypeInfo {
    pub fn size_of() -> uint;
    pub fn size_of_val(&self) -> uint;
}

impl<T> TypeInfo for T {
    /// The size of the type in bytes.
    pub fn size_of() -> uint {
        unsafe { intrinsics::size_of::<T>() }
    }

    /// Returns the size of the type of `self` in bytes.
    pub fn size_of_val(&self) -> uint {
        TypeInfo::size_of::<T>()
    }
}

fn main() {}

I get the error:

type-info.rs:5:4: 5:26 error: failed to find an implementation of trait TypeInfo for 'a
type-info.rs:5     TypeInfo::size_of::<T>()
                   ^~~~~~~~~~~~~~~~~~~~~~

Commenting out the size_of function, I then get this:

type-info.rs:26:8: 26:30 error: failed to find an implementation of trait TypeInfo for 'a
type-info.rs:26         TypeInfo::size_of::<T>()
                        ^~~~~~~~~~~~~~~~~~~~~~

I would have assumed that seeing as type T affects all types you would be able to use the impled methods even if there was no type constraint specified.

@nikomatsakis
Copy link
Contributor

This is not using associated fns as intended by the current implementation. I think the way to implement this would be:

/// Returns the size of a type
pub fn size_of<T>() -> uint {
    TypeInfo::size_of(None::<T>)
}
trait TypeInfo {
    fn size_of(x: Option<Self>) { ... }
    fn size_of_val(&self) -> uint { ... }
}

@brendanzab
Copy link
Member Author

Ah, that's odd. Because Real::pi::<float>() works. But I'm guessing won't be an issue once we get the improved generic path syntax (see #6894)?

@emberian
Copy link
Member

emberian commented Aug 5, 2013

This now compiles and runs correctly. Needstest

bors added a commit that referenced this issue Aug 15, 2013
Closes #3907
Closes #5493
Closes #4464
Closes #4759
Closes #5666
Closes #5884
Closes #5926
Closes #6318
Closes #6557
Closes #6898
Closes #6919
Closes #7222
flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 25, 2021
…-fn, r=giraffate

Fix false-positive `manual_unwrap_or` inside const fn

Fixes rust-lang#6898

changelog:  Fix false-positive for manual_unwrap_or in const fn.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants