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

Option->Result->Option used in ?-on-Option isn't a no-op #49437

Closed
scottmcm opened this issue Mar 28, 2018 · 1 comment
Closed

Option->Result->Option used in ?-on-Option isn't a no-op #49437

scottmcm opened this issue Mar 28, 2018 · 1 comment
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

(Very similar to the rebuild-the-same-Result in #38349, but a bit more complex.)

pub fn foo(x: Option<i32>) -> Result<i32, std::option::NoneError> {
    match x {
        Some(v) => Ok(v),
        None => Err(std::option::NoneError),
    }
}

pub fn bar(x: Option<i32>) -> Option<i32> {
    match foo(x) {
        Ok(v) => Some(v),
        Err(_) => None,
    }
}

Ideally foo would just ^= 1 the discriminant, and bar would be a nop, but today

; playground::foo
; Function Attrs: norecurse nounwind readnone uwtable
define i64 @_ZN10playground3foo17hd0a7b7996c801050E(i64) unnamed_addr #0 {
start:
  %abi_cast.sroa.4.0.extract.shift = and i64 %0, -4294967296
  %1 = and i64 %0, 4294967295
  %switch = icmp eq i64 %1, 0
  %. = zext i1 %switch to i64
  %_0.sroa.0.0.insert.insert = or i64 %abi_cast.sroa.4.0.extract.shift, %.
  ret i64 %_0.sroa.0.0.insert.insert
}

; playground::bar
; Function Attrs: norecurse nounwind readnone uwtable
define i64 @_ZN10playground3bar17h7d7c40401a529d3eE(i64) unnamed_addr #0 {
start:
  %1 = and i64 %0, 4294967295
  %switch.i = icmp ne i64 %1, 0
  %abi_cast1.sroa.4.0.extract.shift = and i64 %0, -4294967296
  %. = zext i1 %switch.i to i64
  %_0.sroa.0.0.insert.insert = or i64 %abi_cast1.sroa.4.0.extract.shift, %.
  ret i64 %_0.sroa.0.0.insert.insert
}

https://play.rust-lang.org/?gist=dc3fab4d8a20e31552769a9a03504c4c&version=nightly&mode=release

@scottmcm scottmcm added I-slow Issue: Problems and improvements with respect to performance of generated code. C-enhancement Category: An issue proposing an enhancement or a PR with one. A-codegen Area: Code generation labels Mar 28, 2018
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 20, 2020
@scottmcm
Copy link
Member Author

scottmcm commented Jun 9, 2021

This references the old ? desugaring, so closing in favour of #85133

@scottmcm scottmcm closed this as completed Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants