Skip to content

Type bounds are ignored in trait and impl function definitions #24011

Closed
@fhahn

Description

@fhahn

It seems that rustc does not check if the type paramater satisfy the declared type bounds in trait and impl function definitions, but only for bare functions. Rustc fails on the following code with the following, rather confusing, error message: type &mut std::collections::hash::set::HashSet<[u8]> does not implement any method in scope named insert

But I think that rustc should fail at an earlier stage, because [u8] does not implement core::marker::Sized.

use std::collections::HashSet;

struct Bar;

impl Bar {
    fn test(bar: &mut HashSet<[u8]>) {
        let x: [u8; 3] = [1, 2, 3];
        bar.insert(x);
    }
}

trait Foo {
    fn test(&self, _: &mut HashSet<[u8]>);
}

The following code is a more general example and does not depend on any library.

trait Cons {
    fn myFn(&self);
}

struct Foo<T: Cons> {
    field: T
}

trait Kaboom {
    fn foo(&self, x: Foo<u8>) {}
   // this does NOT fail, but Foo<u8> is no valid type
}

struct KaboomStruct;

impl KaboomStruct {
    fn bar(&self, x: Foo<u8>) {}
   // this does NOT fail, but Foo<u8> is no valid type
}

fn foo(x: Foo<u8>) {}
// this fails, beacuse u8 does not implement Cons

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions