-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Can not impl trait from another crate for Vec<T> with T from this crate #24745
Comments
In either case, the work-around / fix is to make a new type that wraps |
I think this is an intentional result of #23086 (RFC 1023). |
I think the rationale is so that Vec<_> could be made to implement the trait later on without breaking backwards compatibility (it would conflict with your implementation of the trait otherwise). |
@Diggsey by “later on”, do you mean “in a future version of the crate that defines |
This error now provides a link to a detailed explanation of E0117 so this issue might be considered fixed? |
what's the reason for this restriction in the 1st place? |
An example use case for this: I have a library that implements an AST, Parser and Serializer for a given DSL. I'd like to keep it lean because that's what people will bundle. I want to have a separate crate that adds serde and allows to serialize and deserialize the AST to/from json. I think that this limitation prevents me from being able to do that. |
If this is allowed, adding if your concern is the additional dependencies, you can use features. |
Sure, features are one option, but if I don't want to mess with the original package and keep it clean, I just end up with something like this: which is a clone of https://github.com/projectfluent/fluent-rs/blob/70b5e8d387f3b836032b42c997c359263b21c8d0/src/syntax/ast.rs but with From/Into. (to serialize FTL->AST->JSON and JSON->AST->FTL back). I'm wondering if it would be possible to write code to automate the creation of a clone of a struct/enum from another crate. Then one could add Serialize/Deserialize on those local struct/enums. Another option would be to mark structs/enums as extensible by other crates, which would allow the author to decide that he wants to let others impl features for them. That would work better in my case, because it would remove the step of converting from fluent::syntax::ast::* into fluent_json::ast::* back and forth. |
With specialization this is not an issue anymore. If some library implements |
The bug is in the error message, which I agree is misleading. It's also true that it might be nice to rejigger the rules in this area, but that's a fairly complex undertaking. |
Here is a standalone test case for the same issue: use std::fmt;
struct Bar;
impl fmt::Display for Vec<Bar> { }
fn main() { } Generates:
I'm not sure of the ideal wording, but one idea would be to have it highlight the "foreign types" (in this example, there is only one, the
I am not sure if that last note is all that useful either. Maybe. It seems very jargon heavy. Anyway, the trick would be getting the highlight to exclude the |
Would it make sense to consider type parameters' boundaries to allow anchoring / scoping impls for non-local types? e.g. use std::fmt;
struct Bar;
trait LocalMarker {}
impl LocalMarker for Bar {}
impl<T: LocalMarker> fmt::Display for Vec<T> { }
fn main() { } |
Call out the types that are non local on E0117 CC rust-lang#24745.
Call out the types that are non local on E0117 CC rust-lang#24745.
Call out the types that are non local on E0117 CC rust-lang#24745.
The current output for this case is:
For the last comment:
For the original report:
@nikomatsakis is there anything else we should do here? |
I know what the issue is and what the fix is, but I don't understand what "define and implement a trait or new type instead" is trying to tell me.
So with all that, should it instead be "consider defining a newtype and implement the trait on it instead" ? |
The original test case now compiles, I believe this was fixed with https://blog.rust-lang.org/2020/01/30/Rust-1.41.0.html#relaxed-restrictions-when-implementing-traits |
Test case:
a.rs
b.rs
In rustc 1.1.0-nightly (90cc830 2015-04-22) (built 2015-04-23):
Note the impl does not reference any types defined in this crate. This is not true,
Bar
is defined is this crate.This indicates a bug either in the coherence rules (this program should be valid and the error not be shown) or in the error message, which is misleading.
CC @nikomatsakis
The text was updated successfully, but these errors were encountered: