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

Correct inference of primitive operand type behind binary operation #68129

Merged
merged 2 commits into from
Feb 15, 2020

Conversation

varkor
Copy link
Member

@varkor varkor commented Jan 11, 2020

Fixes #57447.

r? @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 11, 2020
@varkor varkor force-pushed the infer-binary-operand-behind-reference branch from 932763a to 056d48a Compare January 11, 2020 19:17
@Centril Centril added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 12, 2020
@Centril Centril added this to the 1.42 milestone Jan 12, 2020
@nikomatsakis
Copy link
Contributor

The code seems fine, but I think we probably ought to document the behavior of this part of the type-checker better. This effects the lang spec in the end. Perhaps the reference is the right place? I don't think we have a lot of "framework" for doing so, but I think it's probably a good idea for us to start collecting these sorts of things in the reference.

This is tricky, though, as we don't have any sort of "framework", and it's hard to describe the structure of the rules without that. Still, I wouldn't want to get too hung up on (e.g) figuring out the best way to write typing judgements.

Maybe we should start by documenting in rustc-guide or in an open issue to start?

@varkor
Copy link
Member Author

varkor commented Jan 14, 2020

I've written a comment on a relevant issue in the Rust reference repository.

@varkor
Copy link
Member Author

varkor commented Jan 14, 2020

I find it slightly surprising that x + y and &x + &y work, but &&x + &&y (and similar) do not. This is due to https://github.com/rust-lang/rust/blob/056d48a1bc454dec2186f9242db77af69e493524/src/libcore/internal_macros.rs#L21-L23
which seems a little ad hoc (added in #21227).

@nikomatsakis
Copy link
Contributor

It seems a bit surprising, but then those are the impls that exist, right? (i.e., we don't have a general impl for &T, just for specific types?)

@varkor
Copy link
Member Author

varkor commented Jan 15, 2020

Yes, that's right. It's related, but only tangentially in that the cases look similar from a user's perspective.

@nikomatsakis
Copy link
Contributor

@rfcbot fcp merge

Discussed in today's @rust-lang/lang meeting. The consensus of the meeting was that we should make this change, but we're doing an fcp merge to make it official.

We did think it would be good to add some more tests:

  • To cover other sorts of operators, like <<
  • To test cases where the integer is "indirect", like this one
  • To test cases where there is no expected type (which we expect to error)

@rfcbot
Copy link

rfcbot commented Jan 16, 2020

Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jan 16, 2020
@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jan 23, 2020
@rfcbot
Copy link

rfcbot commented Jan 23, 2020

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 23, 2020
@Centril Centril modified the milestones: 1.42, 1.43 Jan 31, 2020
@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Feb 2, 2020
@rfcbot
Copy link

rfcbot commented Feb 2, 2020

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Feb 2, 2020
@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 3, 2020

📌 Commit 056d48a1bc454dec2186f9242db77af69e493524 has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 3, 2020
@nikomatsakis
Copy link
Contributor

@bors r-

Wait, @varkor, did you add the extra tests we mentioned in the meeting...?

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 3, 2020
@varkor
Copy link
Member Author

varkor commented Feb 3, 2020

@nikomatsakis: no, I was waiting for the FCP to complete. I'll do so soon.

@varkor
Copy link
Member Author

varkor commented Feb 9, 2020

To test cases where there is no expected type (which we expect to error)

Just to clarify, do you mean cases like the following?

let _ = 0 + &0;

This currently passes, as it defaults to i32. Or was a different case supposed to error?

I've added the suggested test cases.

@varkor varkor force-pushed the infer-binary-operand-behind-reference branch from 056d48a to 0276d7a Compare February 9, 2020 01:51
@nikomatsakis
Copy link
Contributor

Good question @varkor, what did I mean... I'm not sure, but the tests seem good.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 14, 2020

📌 Commit 0276d7a has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 14, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Feb 14, 2020
…reference, r=nikomatsakis

Correct inference of primitive operand type behind binary operation

Fixes rust-lang#57447.

r? @nikomatsakis
bors added a commit that referenced this pull request Feb 14, 2020
Rollup of 7 pull requests

Successful merges:

 - #68129 (Correct inference of primitive operand type behind binary operation)
 - #68475 (Use a `ParamEnvAnd<Predicate>` for caching in `ObligationForest`)
 - #68856 (typeck: clarify def_bm adjustments & add tests for or-patterns)
 - #69051 (simplify_try: address some of eddyb's comments)
 - #69128 (Fix extra subslice lowering)
 - #69150 (Follow-up to #68848)
 - #69164 (Update pulldown-cmark dependency)

Failed merges:

r? @ghost
@bors bors merged commit 0276d7a into rust-lang:master Feb 15, 2020
@varkor varkor deleted the infer-binary-operand-behind-reference branch February 15, 2020 18:18
@MightyPork
Copy link

MightyPork commented Apr 27, 2020

I hoped this would fix the ever-present "you need a *" nag, but e.g. this still does not work - is that split to a separate issue? It seems to still be broken for all comparison operators

fn main() {
    let a = 1u16;
    let b = 0u16;
    
    let a_borrowed = &a;
    
    if a_borrowed > b { // expected `&u16`, found `u16`
        println!("hello");
    }
}

@varkor
Copy link
Member Author

varkor commented Apr 27, 2020

This would be a separate issue, but I'm not sure whether or not it's covered by an existing issue.
The reason this doesn't currently work is that we don't callforward_ref_binop https://github.com/rust-lang/rust/blob/a59264b01247836c70e24217e0d346b868387525/src/libcore/internal_macros.rs#L21-L23
for the comparison operators, unlike the arithmetic operators. I'm not sure if this is intentional or not. You're welcome to open an issue: if this discussion already exists, someone else will know about it.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request May 16, 2020
Pkgsrc changes:
 * Bump rust bootstrap version to 1.42.0, except for Darwin/i686 where the
   bootstrap is not (yet?) available.

Upstream changes:

Version 1.43.0 (2020-04-23)
==========================

Language
--------
- [Fixed using binary operations with `&{number}` (e.g. `&1.0`) not having
  the type inferred correctly.][68129]
- [Attributes such as `#[cfg()]` can now be used on `if` expressions.][69201]

**Syntax only changes**
- [Allow `type Foo: Ord` syntactically.][69361]
- [Fuse associated and extern items up to defaultness.][69194]
- [Syntactically allow `self` in all `fn` contexts.][68764]
- [Merge `fn` syntax + cleanup item parsing.][68728]
- [`item` macro fragments can be interpolated into `trait`s, `impl`s,
  and `extern` blocks.][69366]
  For example, you may now write:
  ```rust
  macro_rules! mac_trait {
      ($i:item) => {
          trait T { $i }
      }
  }
  mac_trait! {
      fn foo() {}
  }
  ```
These are still rejected *semantically*, so you will likely receive an error but
these changes can be seen and parsed by macros and
conditional compilation.


Compiler
--------
- [You can now pass multiple lint flags to rustc to override the previous
  flags.][67885] For example; `rustc -D unused -A unused-variables` denies
  everything in the `unused` lint group except `unused-variables` which
  is explicitly allowed. However, passing `rustc -A unused-variables -D unused` denies
  everything in the `unused` lint group **including** `unused-variables` since
  the allow flag is specified before the deny flag (and therefore overridden).
- [rustc will now prefer your system MinGW libraries over its bundled libraries
  if they are available on `windows-gnu`.][67429]
- [rustc now buffers errors/warnings printed in JSON.][69227]

Libraries
---------
- [`Arc<[T; N]>`, `Box<[T; N]>`, and `Rc<[T; N]>`, now implement
  `TryFrom<Arc<[T]>>`,`TryFrom<Box<[T]>>`, and `TryFrom<Rc<[T]>>`
  respectively.][69538] **Note** These conversions are only available when `N`
  is `0..=32`.
- [You can now use associated constants on floats and integers directly, rather
  than having to import the module.][68952] e.g. You can now write `u32::MAX` or
  `f32::NAN` with no imports.
- [`u8::is_ascii` is now `const`.][68984]
- [`String` now implements `AsMut<str>`.][68742]
- [Added the `primitive` module to `std` and `core`.][67637] This module
  reexports Rust's primitive types. This is mainly useful in macros
  where you want avoid these types being shadowed.
- [Relaxed some of the trait bounds on `HashMap` and `HashSet`.][67642]
- [`string::FromUtf8Error` now implements `Clone + Eq`.][68738]

Stabilized APIs
---------------
- [`Once::is_completed`]
- [`f32::LOG10_2`]
- [`f32::LOG2_10`]
- [`f64::LOG10_2`]
- [`f64::LOG2_10`]
- [`iter::once_with`]

Cargo
-----
- [You can now set config `[profile]`s in your `.cargo/config`, or through
  your environment.][cargo/7823]
- [Cargo will now set `CARGO_BIN_EXE_<name>` pointing to a binary's
  executable path when running integration tests or benchmarks.][cargo/7697]
  `<name>` is the name of your binary as-is e.g. If you wanted the executable
  path for a binary named `my-program`you would use
  `env!("CARGO_BIN_EXE_my-program")`.

Misc
----
- [Certain checks in the `const_err` lint were deemed unrelated to const
  evaluation][69185], and have been moved to the `unconditional_panic` and
  `arithmetic_overflow` lints.

Compatibility Notes
-------------------

- [Having trailing syntax in the `assert!` macro is now a hard error.][69548]
  This has been a warning since 1.36.0.
- [Fixed `Self` not having the correctly inferred type.][69340] This incorrectly
  led to some instances being accepted, and now correctly emits a hard error.

[69340]: rust-lang/rust#69340

Internal Only
-------------
These changes provide no direct user facing benefits, but represent significant
improvements to the internals and overall performance of `rustc` and
related tools.

- [All components are now built with `opt-level=3` instead of `2`.][67878]
- [Improved how rustc generates drop code.][67332]
- [Improved performance from `#[inline]`-ing certain hot functions.][69256]
- [traits: preallocate 2 Vecs of known initial size][69022]
- [Avoid exponential behaviour when relating types][68772]
- [Skip `Drop` terminators for enum variants without drop glue][68943]
- [Improve performance of coherence checks][68966]
- [Deduplicate types in the generator witness][68672]
- [Invert control in struct_lint_level.][68725]

[67332]: rust-lang/rust#67332
[67429]: rust-lang/rust#67429
[67637]: rust-lang/rust#67637
[67642]: rust-lang/rust#67642
[67878]: rust-lang/rust#67878
[67885]: rust-lang/rust#67885
[68129]: rust-lang/rust#68129
[68672]: rust-lang/rust#68672
[68725]: rust-lang/rust#68725
[68728]: rust-lang/rust#68728
[68738]: rust-lang/rust#68738
[68742]: rust-lang/rust#68742
[68764]: rust-lang/rust#68764
[68772]: rust-lang/rust#68772
[68943]: rust-lang/rust#68943
[68952]: rust-lang/rust#68952
[68966]: rust-lang/rust#68966
[68984]: rust-lang/rust#68984
[69022]: rust-lang/rust#69022
[69185]: rust-lang/rust#69185
[69194]: rust-lang/rust#69194
[69201]: rust-lang/rust#69201
[69227]: rust-lang/rust#69227
[69548]: rust-lang/rust#69548
[69256]: rust-lang/rust#69256
[69361]: rust-lang/rust#69361
[69366]: rust-lang/rust#69366
[69538]: rust-lang/rust#69538
[cargo/7823]: rust-lang/cargo#7823
[cargo/7697]: rust-lang/cargo#7697
[`Once::is_completed`]: https://doc.rust-lang.org/std/sync/struct.Once.html#method.is_completed
[`f32::LOG10_2`]: https://doc.rust-lang.org/std/f32/consts/constant.LOG10_2.html
[`f32::LOG2_10`]: https://doc.rust-lang.org/std/f32/consts/constant.LOG2_10.html
[`f64::LOG10_2`]: https://doc.rust-lang.org/std/f64/consts/constant.LOG10_2.html
[`f64::LOG2_10`]: https://doc.rust-lang.org/std/f64/consts/constant.LOG2_10.html
[`iter::once_with`]: https://doc.rust-lang.org/std/iter/fn.once_with.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Binary operations involving &1.0 always infer f64
8 participants