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

Don't use match-destructuring for derived ops on structs. #98446

Merged
merged 3 commits into from
Jul 4, 2022

Commits on Jul 4, 2022

  1. Configuration menu
    Copy the full SHA
    18f8495 View commit details
    Browse the repository at this point in the history
  2. Comment fixes.

    Remove an out-of-date sentence, and fix a typo.
    nnethercote committed Jul 4, 2022
    Configuration menu
    Copy the full SHA
    528343f View commit details
    Browse the repository at this point in the history
  3. Don't use match-destructuring for derived ops on structs.

    All derive ops currently use match-destructuring to access fields. This
    is reasonable for enums, but sub-optimal for structs. E.g.:
    ```
    fn eq(&self, other: &Point) -> bool {
        match *other {
    	Self { x: ref __self_1_0, y: ref __self_1_1 } =>
    	    match *self {
    		Self { x: ref __self_0_0, y: ref __self_0_1 } =>
    		    (*__self_0_0) == (*__self_1_0) &&
    			(*__self_0_1) == (*__self_1_1),
    	    },
        }
    }
    ```
    This commit changes derive ops on structs to use field access instead, e.g.:
    ```
    fn eq(&self, other: &Point) -> bool {
        self.x == other.x && self.y == other.y
    }
    ```
    This is faster to compile, results in smaller binaries, and is simpler to
    generate. Unfortunately, we have to keep the old pattern generating code around
    for `repr(packed)` structs because something like `&self.x` (which doesn't show
    up in `PartialEq` ops, but does show up in `Debug` and `Hash` ops) isn't
    allowed. But this commit at least changes those cases to use let-destructuring
    instead of match-destructuring, e.g.:
    ```
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
        {
    	let Self(ref __self_0_0) = *self;
    	{ ::core::hash::Hash::hash(&(*__self_0_0), state) }
        }
    }
    ```
    There are some unnecessary blocks remaining in the generated code, but I
    will fix them in a follow-up PR.
    nnethercote committed Jul 4, 2022
    Configuration menu
    Copy the full SHA
    ecc6e95 View commit details
    Browse the repository at this point in the history