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

Turn constants in modules core::[i/u][8/16/32/64/size] into associated constants #28656

Closed
wants to merge 3 commits into from

Conversation

petrochenkov
Copy link
Contributor

Module versions of the constants are left in place for both stability reasons and as an escape hatch for #28586

After this change (and after the resolution of #28586) primitive types [i/u][8/16/32/64/size] and bool can be put into the prelude and not treated specially in resolve (the same can't be done backward-compatibly with f[32/64], char and str, unfortunately).

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
#[unstable(feature = "num_bits_bytes",
reason = "may want to be an associated function",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason needs update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It still may want to be an associated function, just like std::usize::BYTES.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this reason was written down long before we even had associated constants, but with those I don't think we'd want both a constant and a function, and in today's Rust these are definitely most appropriate as a constant.

@bluss
Copy link
Member

bluss commented Sep 25, 2015

Issue #28586 means this may cause ICEs (const is ok in array sizes but associated consts are not).

@bluss
Copy link
Member

bluss commented Sep 25, 2015

Needs [breaking-change] in commit or merge log.

@petrochenkov
Copy link
Contributor Author

Issue #28586 means this may cause ICEs (const is ok in array sizes but associated consts are not).

That issue is interesting, because there is no associated constant BYTES in usize currently. But yeah, the ICE happens with existing associated constants too.

@bluss
Copy link
Member

bluss commented Sep 25, 2015

Maybe the regular const can stay (deprecated) until the issue is resolved? It may be an annoying limitation otherwise. I realize it's only unstable, but still.

@petrochenkov
Copy link
Contributor Author

Yes, that would be reasonable. Updated.

@bors
Copy link
Contributor

bors commented Sep 25, 2015

☔ The latest upstream changes (presumably #28610) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor Author

Rebased.

let &(ref fqp, short) = cache.paths.get(&did).unwrap();
if i > 0 {
try!(write!(&mut w, ","));
if let Some(&(ref fqp, short)) = cache.paths.get(&did) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this was necessary? It seems like this may indicate a bug in rustdoc rather than something that should be ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's certainly a rustdoc bug, I spent may be a couple of hours trying to fix it, but haven't found out what's wrong.

@alexcrichton
Copy link
Member

I'm also a little wary of deprecating an unstable feature in favor of something which may not work in all situations (e.g. those who migrate may end up hitting ICEs). I think it'd be fine to introduce these as associated constants, however in addition to leaving the previous const values there so we can get experience with both.

@petrochenkov
Copy link
Contributor Author

Given that #28586 is a hard issue and is unlikely to be resolved soon, I'm ok with undeprecating.

@petrochenkov
Copy link
Contributor Author

Updated.

@alexcrichton
Copy link
Member

Thanks! Just a few more questions:

  • Could the rustdoc change be investigated and looked into? Rustdoc tends to always limp along but it's probably best if we at least don't knowingly do so!
  • What're the implications of having a stable associated constant? It's still unstable to use, right?
  • Could the unstable messages for the associated constants be updated? (as @bluss mentioned)

Needs [breaking-change] in commit or merge log.

@bluss could you elaborate on what the breaking change is? Was it the deprecation? On the surface this looks like a backwards-compatible change to me, but I haven't worked too closely with associated constants yet!

@bluss
Copy link
Member

bluss commented Sep 29, 2015

Don't think there is any breaking change anymore. It was the removal of the old unstable consts, that turned into deprecation and now no change.

@bluss
Copy link
Member

bluss commented Sep 29, 2015

That said I don't know how code will respond to std::usize; usize::BITS; where it could be both a const and an assoc. const.

@petrochenkov
Copy link
Contributor Author

I don't know how code will respond to std::usize; usize::BITS; where it could be both a const and an assoc. const

It is resolved to std::usize::BITS in this case.

Could the rustdoc change be investigated and looked into?

If I could do it in reasonable time, I'd fix it, but I can't (as I said, I've already spent too much time on it). I can fill an issue and someone (maybe future me) will fix it later.

What're the implications of having a stable associated constant? It's still unstable to use, right?

I need to investigate.

@petrochenkov
Copy link
Contributor Author

What're the implications of having a stable associated constant? It's still unstable to use, right?

Associated constants can be used on stable, but not defined.

@alexcrichton
Copy link
Member

Hm ok, in that case I'd be more comfortable if these stayed as unstable for now to ensure we don't let usage accidentally leak in (I don't think we have many other stable associated constants).

Given that, though, can you remind me what the motivation for this change is? We'd definitely want to make the switch once associated constants are stable, but while they're in limbo we may want to be cautious about exposing and using them. You mention we can make some changes to resolve, but can you elaborate on that as well?

@petrochenkov
Copy link
Contributor Author

@alexcrichton

You mention we can make some changes to resolve, but can you elaborate on that as well?

Primitive types are currently special-cased in resolve. In particular, this special casing implies weird shadowing rules. They are so weird they have to be guarded by a hack (#22093). This is all bad (rust-lang/rfcs#907 (comment)). Ideally, primitive types should live in the prelude and be resolved as all other types. If the new primitive types are added (f16, [u/i]128) they will have to be added this way. But it can't be done backward compatibly for existing types, because some people were careless enough to stabilize modules with names conflicting with primitive types and lots of various items in them. However, integer types can likely be recovered, because their respective modules contain only constants, and constants can ideally be replaced with associated constants and mod i32 {} can be replaced with type i32 = core::prelude::i32 (more details in #20427 (comment) and below). Unfortunately, associated constants can't be used in all places where free constants can be used, because they are resolved too late, and fixing it will probably require a major compiler overhaul.

Sooo... The only remaining motivation for this change is ability to use i32::MAX more conveniently without importing any modules.

@alexcrichton
Copy link
Member

Hm ok, I must admit that I'm not very familiar with the resolve parts of this, but from what it sounds like the benefits of this may be pretty small by this point? If major overhauls are required before associated constants are usable like constants today it may be best to wait on this change?

@petrochenkov
Copy link
Contributor Author

Ok, it's not urgent.

@petrochenkov petrochenkov deleted the primod branch November 22, 2015 11:43
@bluss
Copy link
Member

bluss commented Mar 31, 2016

Can this be revived now?

@petrochenkov
Copy link
Contributor Author

Can this be revived now?

Yes, I think. Associated constants are in much better state now and we have crater to check for breakage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants