Files
rust/tests/ui/higher-ranked/higher-ranked-encoding.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.1 KiB
Rust
Raw Normal View History

2025-06-13 02:30:47 +05:00
//! Regression test for https://github.com/rust-lang/rust/issues/15924
2025-06-13 02:30:47 +05:00
//@ run-pass
2014-10-02 21:52:06 +02:00
2022-05-05 18:34:13 +02:00
use std::marker::PhantomData;
trait Encoder {
type Error;
}
trait Encodable<S: Encoder> {
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
}
impl<S: Encoder> Encodable<S> for i32 {
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
Ok(())
}
}
struct JsonEncoder<'a>(PhantomData<&'a mut ()>);
impl Encoder for JsonEncoder<'_> {
type Error = ();
}
2025-06-13 02:30:47 +05:00
// This function uses higher-ranked trait bounds, which previously caused ICE
fn encode_json<T: for<'r> Encodable<JsonEncoder<'r>>>(object: &T) -> Result<String, ()> {
2022-05-05 18:34:13 +02:00
let s = String::new();
{
let mut encoder = JsonEncoder(PhantomData);
object.encode(&mut encoder)?;
}
Ok(s)
}
2014-10-02 21:52:06 +02:00
2025-06-13 02:30:47 +05:00
// Structure with HRTB constraint that was problematic
2022-05-05 18:34:13 +02:00
struct Foo<T: for<'a> Encodable<JsonEncoder<'a>>> {
2014-10-02 21:52:06 +02:00
v: T,
}
2025-06-13 02:30:47 +05:00
// Drop implementation that exercises the HRTB bounds
2022-05-05 18:34:13 +02:00
impl<T: for<'a> Encodable<JsonEncoder<'a>>> Drop for Foo<T> {
2014-10-02 21:52:06 +02:00
fn drop(&mut self) {
2025-06-13 02:30:47 +05:00
let _ = encode_json(&self.v);
2014-10-02 21:52:06 +02:00
}
}
fn main() {
2015-01-25 22:05:03 +01:00
let _ = Foo { v: 10 };
2014-10-02 21:52:06 +02:00
}