Repository navigation
Trait bounds are not yet enforced in type definitions #21903
Copy link
Copy link
Open
Labels
A-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-future-incompatibilityCategory: Future-incompatibility lintsCategory: Future-incompatibility lintsE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.I-needs-decisionIssue: In need of a decision.Issue: In need of a decision.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.fixed-by-retained-type-aliasesLowering to free alias types (as done by e.g., feature `checked_type_aliases`) fixes this issue.Lowering to free alias types (as done by e.g., feature `checked_type_aliases`) fixes this issue.
Description
Opening an issue since I could not find any information on when/if this will be enforced. Would be really useful for some generalizing I am trying for parser-combinators which I can't do currently without complicating the API.
Specifically I'd like to use a trait bound in a type definition to access an associated type to avoid passing it seperately but due to this issue I simply get a compiler error.
Simplified code which exhibits the error:
trait Stream {
type Item;
}
trait Parser {
type Input: Stream;
}
fn test<T>(u: T) -> T
where T: Parser {
panic!()
}
type Res<I: Stream> = (I, <I as Stream>::Item);
impl <'a> Stream for &'a str { type Item = char; }
impl <I> Parser for fn (I) -> Res<I> where I: Stream { type Input = I; }
//Works
//fn f(_: &str) -> (&str, char) {
//Errors
fn f(_: &str) -> Res<&str> {
panic!()
}
fn main() {
let _ = test(f as fn (_) -> _);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-future-incompatibilityCategory: Future-incompatibility lintsCategory: Future-incompatibility lintsE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.I-needs-decisionIssue: In need of a decision.Issue: In need of a decision.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.fixed-by-retained-type-aliasesLowering to free alias types (as done by e.g., feature `checked_type_aliases`) fixes this issue.Lowering to free alias types (as done by e.g., feature `checked_type_aliases`) fixes this issue.
Type
Projects
- StatusShow more project fieldsIdea
- StatusShow more project fieldsFixed By
Activity
jroesch commentedon Feb 3, 2015
At first glance this is probably related to the internal messy-ness of bounds vs. where-clauses. I'm not sure if the predicate constraints for type aliases are propagated correctly. You should be able to use a newtype as a work around:
Seems to work just fine on playground with the newtype: http://is.gd/krOTZv.
cc @nikomatsakis
Marwes commentedon Feb 3, 2015
I would use a newtype but since the type I want to define is a specialized Result type that isn't possible in my case. I didn't really mention that since it is tangential to the bug itself.
tamird commentedon Apr 22, 2015
Looks like warnings are now generated for this case, but not consistently[0].
Should this be a compile-time error instead?
[0] #20222 (comment)
Marwes commentedon Aug 9, 2015
What is the status on trait bounds in type definitions? I worked around it the best I could for combine but it won't be as clean as it could be until bounds are allowed.
bluss commentedon Nov 26, 2015
Where bounds are allowed, they're not checked properly.
I'm using this one and it compiles from Rust 1.0 or later
nikomatsakis commentedon Dec 15, 2015
Nominating for discussion. Seems like backcompat hazard we should consider trying to address with a feature gate.
bluss commentedon Dec 15, 2015
Since the bounds provide writing types you couldn't do otherwise, they are very useful. A feature gate would be a breaking change without recourse(?).
nikomatsakis commentedon Dec 17, 2015
triage: P-medium
I think this might be not that hard to fix though!
nixpulvis commentedon Feb 9, 2016
Unless I'm missing something, this isn't a huge problem because bad things will still fail to type check right? For example the following doesn't compile.
pnkfelix commentedon Feb 9, 2016
@nixpulvis I think the argument for checking type definitions is analogous to our philosophy about other polymorphic code: we don't do C++ style post template-instantiation type-checking; instead we check the polymorphic code at the definition site (even though both strategies are sound)
Or maybe you are just saying the backwards incompatibility hit won't be that bad? (To which I say: famous last words. .. ;)
nixpulvis commentedon Feb 9, 2016
@pnkfelix I was really just posting to confirm that I wasn't missing something. The warning sounds a bit scarier than it is in reality. I think this is tangental to #30503, as I do feel having type aliases which aren't check could be useful.
30 remaining items
cedricschwyter commentedon May 19, 2022
this would be such a juicy feature, is there any chance this will ever be implemented?
Skallwar commentedon Jun 10, 2022
I just encounter a case where depending on conditional compilation, my type alias could change but I wanted to make sure that whatever concrete type was chosen, at the end it will implement a trait. I posted about it here
I wanted to do something like this:
rustdoc: list matching impls on type aliases
fmease commentedon Aug 25, 2023
To give an update to the people subscribed to this issue, the (incomplete) feature
lazy_type_aliasimplements the expected semantics for type aliases:The feature is nominated for stabilization in a future edition (202X). Its tracking issue #112792 basically supersedes this issue in my humble opinion. See also https://github.com/rust-lang/rust/labels/F-lazy_type_alias.
Rollup merge of rust-lang#115201 - notriddle:notriddle/type-alias-imp…
Unrolled build for rust-lang#115201
Merge pull request rust-lang#21903 from A4-Tacks/postfix-include-nots
Merge pull request rust-lang#21903 from A4-Tacks/postfix-include-nots