-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Inherent associated types: Better type inference #108430
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; | |
use rustc_hir::intravisit::{self, Visitor}; | ||
use rustc_hir::{GenericParamKind, Node}; | ||
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; | ||
use rustc_infer::traits::ObligationCause; | ||
use rustc_infer::traits::{ObligationCause, PredicateObligations}; | ||
use rustc_middle::hir::nested_filter; | ||
use rustc_middle::ty::query::Providers; | ||
use rustc_middle::ty::util::{Discr, IntTypeExt}; | ||
|
@@ -516,6 +516,10 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> { | |
// There's no place to record types from signatures? | ||
} | ||
|
||
fn register_predicate_obligations(&self, _: PredicateObligations<'tcx>) { | ||
// There's no place to track this, so just let it go. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't right. We should at least ICE here. |
||
} | ||
|
||
fn infcx(&self) -> Option<&InferCtxt<'tcx>> { | ||
None | ||
} | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// check-pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this test is still useful since it's kind of superseded by |
||
|
||
#![feature(inherent_associated_types)] | ||
#![allow(incomplete_features)] | ||
|
||
struct Cont<T>(T); | ||
|
||
impl<T: Copy> Cont<T> { | ||
type Out = Vec<T>; | ||
} | ||
|
||
pub fn weird<T: Copy>(x: T) { | ||
let _: Cont<_>::Out = vec![true]; | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![feature(inherent_associated_types)] | ||
#![allow(incomplete_features)] | ||
|
||
struct S<T>(T); | ||
|
||
impl<T> S<T> { type P = (); } | ||
|
||
fn main() { | ||
// There is no way to infer this type. | ||
let _: S<_>::P = (); //~ ERROR type annotations needed | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Testing inference capabilities. | ||
// check-pass | ||
|
||
#![feature(inherent_associated_types)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::convert::identity; | ||
|
||
struct Container<T>(T); | ||
|
||
impl Container<u32> { | ||
type Sink = (); | ||
} | ||
|
||
impl<Any> Container<Any> { | ||
type Thing = Any; | ||
} | ||
|
||
impl<T> Container<(T, ())> { | ||
type Output = ((), Wrapped<T>); | ||
} | ||
|
||
fn main() { | ||
// Inferred via the Self type of the impl. | ||
let _: Container<_>::Sink; | ||
|
||
// Inferred via the RHS: | ||
|
||
let _: Container<_>::Thing = 0; | ||
|
||
let _: Container<Wrapped<_>>::Thing = Wrapped(false); | ||
|
||
let _: Container<_>::Output = (drop(1), Wrapped("...")); | ||
|
||
let binding: Container<_>::Thing = Default::default(); // unsolved at this point | ||
identity::<String>(binding); // constrained and solved here | ||
} | ||
|
||
struct Wrapped<T>(T); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#![feature(inherent_associated_types)] | ||
#![allow(incomplete_features)] | ||
|
||
struct S<T>(T); | ||
|
||
impl<T: Copy> S<T> { | ||
type T = T; | ||
} | ||
|
||
fn main() { | ||
let _: S<_>::T = String::new(); //~ ERROR the trait bound `String: Copy` is not satisfied | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0277]: the trait bound `String: Copy` is not satisfied | ||
--> $DIR/unsatisfied-bounds-inferred-type.rs:11:12 | ||
| | ||
LL | let _: S<_>::T = String::new(); | ||
| ^^^^^^^ the trait `Copy` is not implemented for `String` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be fixed by representing it as a projection, so we see the
Self
type during wfcheck.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do plan on implementing something akin to
InherentProjection
at least experimentally as we've already discussed here. Since I haven't looked into it that much yet, I wasn't sure if this covers wf-checking and inference for theSelf
type or just for the parameters & bounds of an inherent GAT.