Stream: wg-traits

Topic: Inference with integer variables


Florian Diebold (Dec 19 2019 at 18:53, on Zulip):

This came up when implementing Index support in rust-analyzer: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f3f417b1731dcd6e09970651d8f5f084

It seems that rustc detects that since the parameter type is some kind of integer, only the u32 impl can apply, and it infers that type. This won't be possible with current Chalk, since it has no concept of integer variables, right?

nikomatsakis (Dec 19 2019 at 19:18, on Zulip):

Yes and no

nikomatsakis (Dec 19 2019 at 19:18, on Zulip):

I've been wondering about this as I started to draft some notes for the "type library"

nikomatsakis (Dec 19 2019 at 19:19, on Zulip):

Probably the easiest thing to do is indeed to extend with a "kind" of type variable that can only be an integral type, but I was wondering if we could model it via predicates too

nikomatsakis (Dec 19 2019 at 19:19, on Zulip):

(e.g., some kind of IsIntegral(T) predicate that is true for u32 and friends)

nikomatsakis (Dec 19 2019 at 19:19, on Zulip):

It's a bit annoying since I don't really want to make integral types so special but there might be nothing for it

nikomatsakis (Dec 19 2019 at 19:20, on Zulip):

(There is also an analogous situation around floating point types; we have special variables for them in rustc too)

Florian Diebold (Dec 19 2019 at 19:43, on Zulip):

I think the problem with just having a predicate would be that Chalk would have to iterate through all impls to find potentially matching ones

Florian Diebold (Dec 19 2019 at 19:43, on Zulip):

if it knows the set of possible types, it could specifically look for them

Florian Diebold (Dec 19 2019 at 19:43, on Zulip):

although I guess the impl search could also be extended to know about the predicates

nikomatsakis (Dec 19 2019 at 22:04, on Zulip):

Certainly having variables that cannot be unified with non-integral types would let us reject faster; the compiler still (I think) iterates through all possible impls in this case, but it is able to reject them relatively quickly.

nikomatsakis (Dec 19 2019 at 22:04, on Zulip):

I'm wondering if there is some suitable "generalization" of this concept

Last update: Jun 07 2020 at 10:35UTC