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

Traits can be inherited multiple times without rustc throwing an error #3953

Closed
brendanzab opened this issue Nov 13, 2012 · 3 comments
Closed
Labels
A-trait-system Area: Trait system
Milestone

Comments

@brendanzab
Copy link
Member

The code below compiles and executes fine:

use cmp::Eq;

trait Hahaha: Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq,
              Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq,
              Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq,
              Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq,
              Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq,
              Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq, Eq {}

enum Lol = int;

pub impl Lol: Hahaha {
    pure fn eq(other: &Lol) -> bool { *self != **other }
    pure fn ne(other: &Lol) -> bool { *self == **other }
}

fn main() {
    if Lol(2) == Lol(4) {
        io::println("2 == 4");
    } else {
        io::println("2 != 4");
    }
}

Surely rustc should throw an error...?

@ghost ghost assigned catamorphism Nov 15, 2012
@catamorphism
Copy link
Contributor

I have a fix, just testing it.

@nikomatsakis
Copy link
Contributor

I am not sure that this is a bug. Depending on the rules we settle on, it might be reasonable to allow (for example) trait ByWordOrByChar : Iterable<~str>, Iterable<char>. I had originally thought to forbid such things but @pcwalton made a good case that they are ok. Also, you can easily get multiple inheritance of the same trait (e.g., Eq) like so:

trait A : Eq {}
trait B : Eq {}
trait C : A, B {}

and trait semantics are designed to ensure that this has a reasonable meaning (as indeed does C : Eq, Eq).

@catamorphism
Copy link
Contributor

My feeling is that while C: A, B as in your example is reasonable, C: Eq, Eq would never be intentional. I'm not sure if there is some situation where automatically generated code would look like this. Until we have a formal spec of the trait system, though, my inclination is to keep the patch. If someone else runs into it trying to do something that should be allowed, they'll make noise :-)

RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 15, 2024
Fixed pthread_getname_np impl for glibc

The behavior of `glibc` differs a bit different for `pthread_getname_np` from other implementations. It requires the buffer to be at least 16 bytes wide without exception.

[Docs](https://www.man7.org/linux/man-pages/man3/pthread_setname_np.3.html):

```
The pthread_getname_np() function can be used to retrieve the
name of the thread.  The thread argument specifies the thread
whose name is to be retrieved.  The buffer name is used to return
the thread name; size specifies the number of bytes available in
name.  The buffer specified by name should be at least 16
characters in length.  The returned thread name in the output
buffer will be null terminated.
```

[Source](https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getname.c;hb=dff8da6b3e89b986bb7f6b1ec18cf65d5972e307#l37):

```c
int
__pthread_getname_np (pthread_t th, char *buf, size_t len)
{
  const struct pthread *pd = (const struct pthread *) th;

  /* Unfortunately the kernel headers do not export the TASK_COMM_LEN
     macro.  So we have to define it here.  */
#define TASK_COMM_LEN 16
  if (len < TASK_COMM_LEN)
    return ERANGE;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system
Projects
None yet
Development

No branches or pull requests

3 participants