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

Clarify and document operator overloading trait matching rules etc #10337

Closed
nikomatsakis opened this issue Nov 7, 2013 · 6 comments
Closed
Assignees

Comments

@nikomatsakis
Copy link
Contributor

Our current trait matching rules for operator overloading are somewhat ad-hoc. I've got a branch which implements a more straightforward set of rules, which I think is what we want for 1.0 going forward. Part of completing this bug will be updating the docs.

The rules I've in mind can be summarized as:

Given an expression l op r where op is a binary operator and l : L and r : R:

  • We search for a trait impl Op<R,T> for L. Note that there is no attempt to autoderef l and so forth.
  • At runtime we'll pass in &l and &r to the trait method (as is reflected in the &self type etc).
  • The type of l op r is T.
@nikomatsakis
Copy link
Contributor Author

Nominating for 1.0.

@pnkfelix
Copy link
Member

pnkfelix commented Nov 7, 2013

Accepted for 1.0, P-backcompat-lang.

@nikomatsakis
Copy link
Contributor Author

It occurs to me that if we make these changes we could avoid the need to know the type of x in x + 1. Basically we could just call it a plain trait match, and then add lang items for the built-in additions (x + 1 etc). Essentially all operators would be overloaded. The usual reason we haven't done this is that operations on ints and so forth are privileged in that they can appear in constant expressions. But there is no reason that we can't say that operations in constant expressions must resolve to lang items. It would also simplify a lot of other code if all operators were considered overloaded (up until trans, where I imagine we'd inline + and so forth rather than leaning on LLVM for that).

@nikomatsakis
Copy link
Contributor Author

See #8280 for an example where this would help.

@flaper87
Copy link
Contributor

flaper87 commented Apr 1, 2014

visiting for triage. Nothing to add here

@alexcrichton
Copy link
Member

Given https://github.com/rust-lang/rfcs/blob/master/text/0439-cmp-ops-reform.md and the fact that operators are no longer taking by reference, I believe this issue is resolved, so closing.

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
Order auto-imports by relevance

Fixes rust-lang#10337.

Basically we sort the imports according to how "far away" the imported item is from where we want to import it to. This change makes it so that imports from the current crate are sorted before any third-party crates. Additionally, we make an exception for builtin crates (`std`, `core`, etc.) so that they are sorted before any third-party crates.

There are probably other heuristics that should be added to improve the experience (such as preferring imports that are common elsewhere in the same crate, and ranking crates depending on the dependency graph). However, I think this is a first good step.

PS. This is my first time contributing here, so please be gentle if I have missed something obvious :-)
flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2023
Fix: Some suggestions generated by the option_if_let_else lint did not compile

This addresses a bug in Clippy where the fix suggestend by the `option_if_let_else` lint would not compile for `Result`s which have an impure expression in the `else` branch.

---

changelog: [`option_if_let_else`]: Fixed incorrect suggestion for `Result`s
[rust-lang#10337](rust-lang/rust-clippy#10337)
<!-- changelog_checked -->

Fixes rust-lang#10335.
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

No branches or pull requests

4 participants