-
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
RFC 213: Implement Default Type Parameter Fallback #26870
Conversation
\o/ |
|
||
// Here, B is instantiated with $1=uint, and constraint $0 <: $1 is added. | ||
bar(x); | ||
} |
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.
Error message?
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 was working on improving it this afternoon/evening but got side tracked by trying to get type aliases to work correctly. Going to try to finish (and then add it here) it tomorrow morning PST.
Is it a breaking change for code involving default type parameters in struct declarations? I guess not. Defaults in impl blocks? Defaults in methods and functions' type parameters? (Yes, I guess it must be). The rationale is that type parameter defaults never did anything really for methods and functions, so it's not breaking a previously working feature(?) |
Default type parameters allow a form of static function parameters with defaults. Imagine |
Definitely would like a crater build of this change just to see what the impact would be. I don't expect much breakage in practice. |
@@ -98,6 +98,9 @@ pub struct InferCtxt<'a, 'tcx: 'a> { | |||
normalize: bool, | |||
|
|||
err_count_on_creation: usize, | |||
|
|||
// Default Type Parameter fallbacks | |||
pub defaults: RefCell<FnvHashMap<Ty<'tcx>, Ty<'tcx>>>, |
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.
So, I'm concerned about this map.
For one thing, I'd want to "encapsulate it more":
a. make this private
b. key it by ty::TyVid
c. insert new data by adding an (optional) parameter to next_ty_var
, rather than mutating the table after the fact
But a bigger issue is that it should be versioned for transactions. As it is, we are going to create fresh type variables with defaults then (potentially) roll them back, which might clear the default. I think my preference would be to remove the map, and instead store the Option<Ty<'tcx>>
as part of the Bounded
variant in type_variable::TypeVariableValue
(after all, who cares what the default was once the type is Known
?)
I'd probably change TypeVariableValue
to something like:
enum TypeVariableValue<'tcx> {
Known(Ty<'tcx>),
Bounded { relations: Vec<Relation>, default: Option<Ty<'tcx>> }
}
(Also, if you do it this way, the transactional problem should just take care of itself.)
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.
Did this refactor, testing now.
More tests that would be nice:
|
CC @apasel422 and @reem for crazy uses of defaults |
☔ The latest upstream changes (presumably #26895) made this pull request unmergeable. Please resolve the merge conflicts. |
d652a37
to
ea007bf
Compare
} | ||
}); | ||
//.subst(self.tcx, &substs) | ||
let ty_var = self.next_ty_var_with_default(default); |
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.
we still are not substituting here, right? I am guessing you'll have problems related to references to other variables, as well as maybe Self
...?
1462a88
to
cae3f61
Compare
Crater says: https://gist.github.com/brson/ce88d850e673ea2c2d05 Two stack overflows. |
cae3f61
to
d1545d3
Compare
2fd7d2b
to
8ea9672
Compare
@bors r+ |
📌 Commit 5ad36cb has been approved by |
This PR completes [RFC 213](https://github.com/rust-lang/rfcs/blob/master/text/0213-defaulted-type-params.md) by allowing default type parameters to influence inference. This is almost certainly a breaking change due to interactions between default type parameters and the old fallback algorithm used for integral and floating point literals. The error messages still require polish but I wanted to get early review and feedback from others on the the changes, error messages, and test cases. I also imagine we will want to run anywhere from 1-3 versions of this on crater and evaluate the impact, and it would be best to get that ball rolling. The only outstanding issue I'm aware of is that type alias defaults don't work. It seems this may require significant restructuring, since during inference type aliases have already been expanded. @nikomatsakis might be able to provide some clarity here. r? @nikomatsakis cc @eddyb @gankro @aturon @brson
⌛ Testing commit 5ad36cb with merge a5c12f4... |
Hm, after this change I still can't implement heterogeneous comparisons for Option without breakage. Reduced case:
|
@petrochenkov Maybe the impl also needs the default? |
@eddyb |
💓 😻 😍 💖 ✨ |
This program works on the compiler built from my branch: // Almost PartialEq
trait PartialQe<Rhs = Self> {
fn qe(&self, other: &Rhs) {}
}
impl<A, B = A> PartialQe<Option<B>> for Option<A> {}
fn main() {
PartialQe::qe(&Some("str"), &None);
Some('a').qe(&None);
} |
Now I see, it works only when the feature gate is enabled.
So, it can't yet be used to generalize standard library facilities, like |
@petrochenkov we are going to let it bake for a cycle in order to give us time to tweak the algorithm, then it will be stabilized. |
This PR completes RFC 213 by allowing default type parameters to influence inference. This is almost certainly a breaking change due to interactions between default type parameters and the old fallback algorithm used for integral and floating point literals.
The error messages still require polish but I wanted to get early review and feedback from others on the the changes, error messages, and test cases. I also imagine we will want to run anywhere from 1-3 versions of this on crater and evaluate the impact, and it would be best to get that ball rolling.
The only outstanding issue I'm aware of is that type alias defaults don't work. It seems this may require significant restructuring, since during inference type aliases have already been expanded. @nikomatsakis might be able to provide some clarity here.
r? @nikomatsakis
cc @eddyb @gankro @aturon @brson