rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.28k stars 12.58k forks source link

Calling intrinsics on unsupported types #108967

Closed Patryk27 closed 1 year ago

Patryk27 commented 1 year ago

Code

#![feature(core_intrinsics)]

fn main() {
    let foo = std::intrinsics::wrapping_sub("oi", "mate");
}

Current output

warning: unused variable: `foo`
 --> src/main.rs:4:9
  |
4 |     let foo = std::intrinsics::wrapping_sub("oi", "mate");
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
  |
  = note: `#[warn(unused_variables)]` on by default

error: internal compiler error: /rustc/39f2657d1101b50f9b71ae460b762d330cc8426b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs:642:17: unexpected fat ptr binop

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/39f2657d1101b50f9b71ae460b762d330cc8426b/compiler/rustc_errors/src/lib.rs:1644:9
stack backtrace:
   0:     0x7fe29469351a - std::backtrace_rs::backtrace::libunwind::trace::hbc48ddb4bcf57554
                               at /rustc/39f2657d1101b50f9b71ae460b762d330cc8426b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fe29469351a - std::backtrace_rs::backtrace::trace_unsynchronized::h9673e7a745f884fe
                               at /rustc/39f2657d1101b50f9b71ae460b762d330cc8426b/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fe29469351a - std::sys_common::backtrace::_print_fmt::hb8bf1b68b7a7c088
                               at /rustc/39f2657d1101b50f9b71ae460b762d330cc8426b/library/std/src/sys_common/backtrace.rs:65:5
...

Desired output

Ideally some error message pointing at wrapping_sub() and saying that it's been used with an unsupported / invalid type.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Patryk27 commented 1 year ago

What's curious, calling it on a reference to a number:

let val = std::intrinsics::wrapping_sub(&1, &2);

... yields a better error, since it at least says Cannot perform arithmetic on type &i32 (although in a weird place):

warning: unused variable: `val`
 --> src/main.rs:4:9
  |
4 |     let val = std::intrinsics::wrapping_sub(&1, &2);
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_val`
  |
  = note: `#[warn(unused_variables)]` on by default

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in Item(WithOptConstParam { did: DefId(0:3 ~ playground[e443]::main), const_param_did: None }) (after phase change to runtime-optimized) at bb0[2]:
                                Cannot perform arithmetic on type &i32
 --> src/main.rs:4:15
  |
4 |     let val = std::intrinsics::wrapping_sub(&1, &2);
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: delayed at    0: <rustc_errors::HandlerInner>::emit_diagnostic
...
workingjubilee commented 1 year ago

If you cannot call an intrinsic without enabling the core_intrinsics or platform_intrinsics feature: That is because it is an implementation detail that is exposed for... uh. Reasons? Possibly not entirely good ones? Possibly to work around some issue like macro expansion.

Feel free to submit a PR documenting correct usage, but we will likely not be going to much effort to make these emit good errors, as you are firmly in "how the sausage is made" territory so it is not very useful to make the sausage grinder be less of a grinder. The acceptable amount of compile time to trade off for catching these kinds of things before they land at a horrible, illegible error is near 0.

Patryk27 commented 1 year ago

I see - nah, in this case it’s not worthwhile to waste any time working on this; thanks for responding 🙂