about summary refs log tree commit diff
path: root/tests/ui/traits/encoder-trait-bounds-regression.rs
blob: 292b921cdf7611d7f496f679377042b60e9c801c (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Regression test for issue #11881
//!
//! Originally, the compiler would ICE when trying to parameterize on certain encoder types
//! due to issues with higher-ranked trait bounds and lifetime inference. This test checks
//! that various encoder patterns work correctly:
//! - Generic encoders with associated error types
//! - Higher-ranked trait bounds (for<'r> Encodable<JsonEncoder<'r>>)
//! - Multiple encoder implementations for the same type
//! - Polymorphic encoding functions

//@ run-pass

#![allow(unused_must_use)]
#![allow(dead_code)]
#![allow(unused_imports)]

use std::io::Cursor;
use std::io::prelude::*;
use std::marker::PhantomData;
use std::{fmt, slice};

trait Encoder {
    type Error;
}

trait Encodable<S: Encoder> {
    fn encode(&self, s: &mut S) -> Result<(), S::Error>;
}

struct JsonEncoder<'a>(PhantomData<&'a mut ()>);

impl Encoder for JsonEncoder<'_> {
    type Error = ();
}

struct AsJson<'a, T> {
    inner: &'a T,
}

impl<'a, T: for<'r> Encodable<JsonEncoder<'r>>> fmt::Display for AsJson<'a, T> {
    /// Encodes a json value into a string
    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Ok(())
    }
}

fn as_json<T>(t: &T) -> AsJson<'_, T> {
    AsJson { inner: t }
}

struct OpaqueEncoder(Vec<u8>);

impl Encoder for OpaqueEncoder {
    type Error = ();
}

struct Foo {
    baz: bool,
}

impl<S: Encoder> Encodable<S> for Foo {
    fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
        Ok(())
    }
}

struct Bar {
    froboz: usize,
}

impl<S: Encoder> Encodable<S> for Bar {
    fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
        Ok(())
    }
}

enum WireProtocol {
    JSON,
    Opaque,
}

fn encode_json<T: for<'a> Encodable<JsonEncoder<'a>>>(val: &T, wr: &mut Cursor<Vec<u8>>) {
    write!(wr, "{}", as_json(val));
}

fn encode_opaque<T: Encodable<OpaqueEncoder>>(val: &T, wr: Vec<u8>) {
    let mut encoder = OpaqueEncoder(wr);
    val.encode(&mut encoder);
}

pub fn main() {
    let target = Foo { baz: false };
    let proto = WireProtocol::JSON;
    match proto {
        WireProtocol::JSON => encode_json(&target, &mut Cursor::new(Vec::new())),
        WireProtocol::Opaque => encode_opaque(&target, Vec::new()),
    }
}