File: error-provide.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (50 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Codegen test for #126242

//@ compile-flags: -O
#![crate_type = "lib"]
#![feature(error_generic_member_access)]
use std::error::Request;
use std::fmt;

#[derive(Debug)]
struct MyBacktrace1 {}

#[derive(Debug)]
struct MyBacktrace2 {}

#[derive(Debug)]
struct MyBacktrace3 {}

#[derive(Debug)]
struct MyError {
    backtrace1: MyBacktrace1,
    backtrace2: MyBacktrace2,
    backtrace3: MyBacktrace3,
    other: MyBacktrace3,
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Example Error")
    }
}

impl std::error::Error for MyError {
    // CHECK-LABEL: @provide
    #[no_mangle]
    fn provide<'a>(&'a self, request: &mut Request<'a>) {
        // LLVM should be able to optimize multiple .provide_* calls into a switch table
        // and eliminate redundant ones, rather than compare one-by-one.

        // CHECK-NEXT: start:
        // CHECK-NEXT: %[[SCRUTINEE:[^ ]+]] = load i64, ptr
        // CHECK-NEXT: switch i64 %[[SCRUTINEE]], label %{{.*}} [
        // CHECK-COUNT-3: i64 {{.*}}, label %{{.*}}
        // CHECK-NEXT: ]
        request
            .provide_ref::<MyBacktrace1>(&self.backtrace1)
            .provide_ref::<MyBacktrace3>(&self.other)
            .provide_ref::<MyBacktrace2>(&self.backtrace2)
            .provide_ref::<MyBacktrace3>(&self.backtrace3);
    }
}