rust-lang / rust-lang/rust

E0210: clarify "at least one type"

Open
#128,814 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coherence T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub trait StreamParser {
    fn new(stream: Box<dyn BufRead>) -> Self;
    fn next_triple(&mut self) -> Result<Option<Arc<Triple>>, SourceError>;
}

pub struct NTriplesParser();

impl StreamParser for NTriplesParser { .. }; // left out for conciseness

pub struct StreamParserIter<P: StreamParser>(P);

impl<P> Iterator for StreamParserIter<P>
where
    P: StreamParser,
{
    type Item = Arc<Triple>;

    fn next(&mut self) -> Option<Self::Item> {
        // FIXME handle errors somehow
        self.0.next_triple().ok()?
    }
}

impl<P> IntoIterator for P
where
    P: StreamParser,
{
    type Item = Arc<Triple>;
    type IntoIter = StreamParserIter<P>;

    fn into_iter(self) -> Self::IntoIter {
        StreamParserIter(self)
    }
}
Current output
error[E0210]: type parameter `P` must be used as the type parameter for some local type (e.g., `MyStruct<P>`)
  --> core/src/sources/graphs/parsers.rs:32:6
   |
32 | impl<P> IntoIterator for P
   |      ^ type parameter `P` must be used as the type parameter for some local type
   |
   = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
   = note: only traits defined in the current crate can be implemented for a type parameter
Desired output
note: implementing a foreign trait is only possible if all of the types for which it is implemented are local
Rationale and extra context

In the above example, clearly one type that satisfies P: StreamParser is local. The real essence here is that some other crate might have at least one type that implements StreamParser. So, in fact, all types hit by the generic trait bound must be local.

Other cases

No response

Rust Version
rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce E0210 with the provided StreamParser and IntoIterator example using rustc 1.80.0, then trace the diagnostic responsible for the note wording in the compiler sources. Done means the output explains that all types involved must be local and matches the requested wording.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.