Skip to content
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

rustc: inference for numeric literals does not take default type parameters into account #15760

Closed
aturon opened this issue Jul 17, 2014 · 14 comments
Labels
A-inference Area: Type inference A-typesystem Area: The type system C-bug Category: This is a bug.

Comments

@aturon
Copy link
Member

aturon commented Jul 17, 2014

The following code fails to compile:

#![feature(default_type_params)]

fn foo<T = u64>(t: T) {}
fn main() {
    foo(0);
}

with

error: cannot determine a type for this expression: cannot determine the type of this integer; add a suffix to specify the type explicitly

despite the fact that the actual type is determinant.

Fixing this problem could help mitigate problems with the int fallback: for functions like range, we could provide a default type parameter of, say, uint while retaining flexibility for other types. (Doing so loses some of the benefit of the fallback removal, but we can introduce defaults in a controlled way.)

@aturon
Copy link
Member Author

aturon commented Jul 17, 2014

cc @wycats, @brson, @alexcrichton

@alexcrichton
Copy link
Member

This seems like it would be nice to have, especially for the range case. If we were to push forward with it, we may want to consider making default type parameters a full-fledged feature of the language, promoting them out of a feature gate.

@thestinger
Copy link
Contributor

A pointer size integer doesn't make much sense as a default for range. It varies in size per platform and will cause lots of problems on 32-bit since developers aren't going to be using it.

@aturon
Copy link
Member Author

aturon commented Jul 18, 2014

@alexcrichton I think we should probably push for un-feature gating anyway, as we're using this feature in other important places in std already.

@thestinger The uint choice was just an example, not carefully thought through. But the key point is, unlike the int fallback, we can make choices about defaults that vary depending on the context.

I do think that providing a default for range would recover a lot of the ergonomics of the int fallback, though. Do you think that proving any default for range is a bad idea? Or just that we should pick a fixed-side default?

@eddyb
Copy link
Member

eddyb commented Jul 27, 2014

I tried implementing a more general form of this, and it's not that hard to hook it up to our existing inference infrastructure.
Sadly, #13702 didn't get any kind of feedback, not even a mention about requiring an RFC.

@aturon
Copy link
Member Author

aturon commented Jul 27, 2014

@eddyb :( I'm sorry to hear that.

I've spoken to @nikomatsakis about the general idea of using default type parameters as a fallback, and he seems to think it's a good idea. If you'd like, we could collaborate on an RFC and then try to revive your PR.

@aturon
Copy link
Member Author

aturon commented Jul 27, 2014

@eddyb Relatedly, I would like to push to un-feature gate default type parameters, as they are proving useful in multiple places in libstd. I think your work in allowing them to work as a fallback will be an important step.

@sanxiyn
Copy link
Member

sanxiyn commented Dec 17, 2014

What happened to this?

@ftxqxd
Copy link
Contributor

ftxqxd commented Dec 17, 2014

AIUI, rust-lang/rfcs#213 proposes this feature, but hasn’t been merged yet.

@steveklabnik
Copy link
Member

This code now compiles.

@eddyb
Copy link
Member

eddyb commented Sep 27, 2015

It only compiles because we default to i32 - presumably you want to use size_of_val or something similar to check the type.

@eddyb eddyb reopened this Sep 27, 2015
@bltavares
Copy link
Contributor

Triage: the OP's code compiles fine on nightly with many warnings:

warning: this feature is stable. attribute no longer needed, #[warn(stable_features)] on by default
 --> <anon>:1:12
1 |> #![feature(default_type_params)]
  |>            ^^^^^^^^^^^^^^^^^^^

warning: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions., #[warn(invalid_type_param_default)] on by default
 --> <anon>:3:8
3 |> fn foo<T = u64>(t: T) {}
  |>        ^
warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
note: for more information, see PR 30742 <https://github.com/rust-lang/rust/pull/30724>

warning: unused variable: `t`, #[warn(unused_variables)] on by default
 --> <anon>:3:17
3 |> fn foo<T = u64>(t: T) {}
  |>                 ^

On stable, without the feature pragma, the warning on default for functions will be a hard error in future release, as mentioned on the nightly warnings.

@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. A-inference Area: Type inference labels Jul 21, 2017
@leoyvens
Copy link
Contributor

@Mark-Simulacrum I think this issue can be closed as a subissue of #27336, the fate of this will be decided by what we do with that RFC. It's certainly not "just a bug".

@Mark-Simulacrum
Copy link
Member

Yeah, that's true. Thanks -- closing.

nivkner added a commit to nivkner/rust that referenced this issue Mar 17, 2018
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 13, 2023
make mir::ProjectionStore-impls pub-accessible

When using RA as a crate the `mir::Place` `projection` is accessible, however there is no way to translate the `ProjectionId` to a `&[PlaceElem]`, as the `ProjectionId::lookup` is private.

Personally, I would only need the `ProjectionId::lookup`-fn to be `pub`, but I don't see any reason why the others should be kept private.. am I missing something `@HKalbasi` ?

Relates to: rust-lang/rust-analyzer#15575
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-typesystem Area: The type system C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

10 participants