| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
#![allow( |
| 4 |
dead_code, |
| 5 |
unreachable_pub, |
| 6 |
clippy::items_after_statements, |
| 7 |
clippy::missing_safety_doc, |
| 8 |
clippy::needless_pass_by_value, |
| 9 |
clippy::no_effect_underscore_binding, |
| 10 |
clippy::undocumented_unsafe_blocks |
| 11 |
)] |
| 12 |
|
| 13 |
use std::{pin::Pin, rc::Rc}; |
| 14 |
|
| 15 |
use easy_ext::ext; |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn simple() { |
| 19 |
#[ext] |
| 20 |
impl str { |
| 21 |
fn foo(&self, pat: &str) -> String { |
| 22 |
self.replace(pat, "_") |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
assert_eq!("--".foo("-"), "__"); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn params() { |
| 31 |
#[ext] |
| 32 |
impl<T, E> Result<T, E> { |
| 33 |
fn err_into<U>(self) -> Result<T, U> |
| 34 |
where |
| 35 |
E: Into<U>, |
| 36 |
{ |
| 37 |
self.map_err(Into::into) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
let err: Result<(), _> = Err(1_u32); |
| 42 |
assert_eq!(err.err_into::<u64>().unwrap_err(), 1_u64); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn lifetime() { |
| 47 |
#[ext(OptionExt)] |
| 48 |
impl<'a, T> &'a mut Option<T> { |
| 49 |
fn into_ref(self) -> Option<&'a T> { |
| 50 |
self.as_ref() |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
let _: Option<&u8> = Some(1).into_ref(); |
| 55 |
} |
| 56 |
|
| 57 |
mod bar { |
| 58 |
use easy_ext::ext; |
| 59 |
|
| 60 |
// assoc-item-level visibility + named |
| 61 |
#[ext(E1)] |
| 62 |
impl str { |
| 63 |
pub const FOO1: &'static str = "_"; |
| 64 |
|
| 65 |
pub fn foo1(&self, pat: &str) -> String { |
| 66 |
self.replace(pat, Self::FOO1) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// assoc-item-level visibility + unnamed |
| 71 |
#[ext] |
| 72 |
impl str { |
| 73 |
pub const FOO2: &'static str = "_"; |
| 74 |
|
| 75 |
pub fn foo2(&self, pat: &str) -> String { |
| 76 |
self.replace(pat, Self::FOO2) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// impl-level visibility + named |
| 81 |
#[ext(E2)] |
| 82 |
pub impl str { |
| 83 |
const FOO3: &'static str = "_"; |
| 84 |
|
| 85 |
fn foo3(&self, pat: &str) -> String { |
| 86 |
self.replace(pat, Self::FOO3) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// impl-level visibility + unnamed |
| 91 |
#[ext] |
| 92 |
pub impl str { |
| 93 |
const FOO4: &'static str = "_"; |
| 94 |
|
| 95 |
fn foo4(&self, pat: &str) -> String { |
| 96 |
self.replace(pat, Self::FOO4) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
pub(super) mod baz { |
| 101 |
use easy_ext::ext; |
| 102 |
|
| 103 |
#[ext(E4)] |
| 104 |
impl str { |
| 105 |
pub(super) fn bar(&self, pat: &str) -> String { |
| 106 |
self.replace(pat, "_") |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[ext(E5)] |
| 111 |
impl str { |
| 112 |
pub fn baz(&self, pat: &str) -> String { |
| 113 |
self.replace(pat, "_") |
| 114 |
} |
| 115 |
|
| 116 |
pub fn baz2(&self, pat: &str) -> String { |
| 117 |
self.replace(pat, "-") |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#[ext(E6)] |
| 122 |
pub(super) impl str { |
| 123 |
fn bar2(&self, pat: &str) -> String { |
| 124 |
self.replace(pat, "_") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
#[ext(E7)] |
| 129 |
pub(crate) impl str { |
| 130 |
fn baz3(&self, pat: &str) -> String { |
| 131 |
self.replace(pat, "_") |
| 132 |
} |
| 133 |
|
| 134 |
fn baz4(&self, pat: &str) -> String { |
| 135 |
self.replace(pat, "-") |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn visibility() { |
| 143 |
use self::bar::{ |
| 144 |
E1, E2, |
| 145 |
baz::{E5, E7}, |
| 146 |
}; |
| 147 |
|
| 148 |
assert_eq!("..".foo1("."), "__"); |
| 149 |
assert_eq!("..".foo3("."), "__"); |
| 150 |
assert_eq!("..".baz("."), "__"); |
| 151 |
assert_eq!("..".baz2("."), "--"); |
| 152 |
assert_eq!("..".baz3("."), "__"); |
| 153 |
assert_eq!("..".baz4("."), "--"); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn generics() { |
| 158 |
#[ext(IterExt)] |
| 159 |
impl<I: IntoIterator> I { |
| 160 |
fn next2(self) -> Option<I::Item> { |
| 161 |
self.into_iter().next() |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
assert_eq!(vec![1, 2, 3].next2(), Some(1_u8)); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn trait_generics() { |
| 170 |
#[derive(Debug, PartialEq, Eq)] |
| 171 |
struct A {} |
| 172 |
|
| 173 |
impl Iterator for A { |
| 174 |
type Item = (); |
| 175 |
fn next(&mut self) -> Option<Self::Item> { |
| 176 |
None |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#[ext(ConstInit)] |
| 181 |
impl A { |
| 182 |
const INIT1: Self = Self {}; |
| 183 |
const INIT2: A = A {}; |
| 184 |
} |
| 185 |
|
| 186 |
#[ext(Ext1)] |
| 187 |
impl<I: Iterator + ConstInit> I { |
| 188 |
const CONST1: Self = Self::INIT1; |
| 189 |
const CONST2: I = I::INIT1; |
| 190 |
type Item2 = Self::Item; |
| 191 |
type Item3 = I::Item; |
| 192 |
fn method1(mut self) -> Option<Self::Item> { |
| 193 |
self.next() |
| 194 |
} |
| 195 |
fn method2(mut self) -> Option<I::Item> { |
| 196 |
self.next() |
| 197 |
} |
| 198 |
fn method3(mut self) -> Option<Self::Item2> { |
| 199 |
self.next() |
| 200 |
} |
| 201 |
fn method4(mut self) -> Option<<I as Ext1>::Item3> { |
| 202 |
self.next() |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
fn a<T: Ext1 + Eq + std::fmt::Debug>(mut x: T) { |
| 207 |
let y = T::CONST1; |
| 208 |
let _ = T::CONST2; |
| 209 |
assert_eq!(x, y); |
| 210 |
assert!(x.next().is_none()); |
| 211 |
} |
| 212 |
|
| 213 |
assert_eq!(A {}.method1(), None); |
| 214 |
assert_eq!(A {}.method2(), None); |
| 215 |
|
| 216 |
a(A::INIT1); |
| 217 |
a(A::INIT2); |
| 218 |
|
| 219 |
#[ext(Ext2)] |
| 220 |
impl<I: Iterator + ConstInit> I { |
| 221 |
const CONST3: I = { |
| 222 |
fn a<I>() {} |
| 223 |
I::INIT1 |
| 224 |
}; |
| 225 |
type Item4 = I::Item; |
| 226 |
fn method5(self, _: I::Item) -> (Option<I::Item>, <I as Ext2>::Item4) { |
| 227 |
fn a<I>() {} |
| 228 |
unimplemented!() |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn type_parameter_defaults() { |
| 235 |
#[ext(Ext)] |
| 236 |
impl<T = ()> () {} |
| 237 |
impl Ext for u8 {} |
| 238 |
|
| 239 |
// The code above is equivalent to the code below. |
| 240 |
|
| 241 |
trait Trait<T = ()> {} |
| 242 |
impl<T> Trait<T> for () {} |
| 243 |
impl Trait for u8 {} |
| 244 |
} |
| 245 |
|
| 246 |
// See also ui/maybe.rs |
| 247 |
#[test] |
| 248 |
fn maybe() { |
| 249 |
#[ext] |
| 250 |
impl<T: ?Sized> T { |
| 251 |
fn f(&self) {} |
| 252 |
} |
| 253 |
|
| 254 |
#[ext] |
| 255 |
impl<T> T |
| 256 |
where |
| 257 |
T: ?Sized, |
| 258 |
{ |
| 259 |
fn f(&self) {} |
| 260 |
} |
| 261 |
|
| 262 |
#[ext] |
| 263 |
impl<T: Send + ?Sized + Sync> T { |
| 264 |
fn f(&self) {} |
| 265 |
} |
| 266 |
|
| 267 |
#[ext] |
| 268 |
impl<T> T |
| 269 |
where |
| 270 |
T: Send + ?Sized + Sync, |
| 271 |
{ |
| 272 |
fn f(&self) {} |
| 273 |
} |
| 274 |
|
| 275 |
#[ext] |
| 276 |
impl<T> T |
| 277 |
where |
| 278 |
T: Iterator, |
| 279 |
T: ?Sized, |
| 280 |
T: Send, |
| 281 |
{ |
| 282 |
fn f(&self) {} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn inline() { |
| 288 |
#[ext] |
| 289 |
impl str { |
| 290 |
#[inline] |
| 291 |
fn auto(&self) {} |
| 292 |
#[inline(always)] |
| 293 |
fn always(&self) {} |
| 294 |
#[inline(never)] |
| 295 |
fn never(&self) {} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn assoc_ty() { |
| 301 |
#[ext(StrExt)] |
| 302 |
impl str { |
| 303 |
type Assoc = String; |
| 304 |
|
| 305 |
fn owned(&self) -> Self::Assoc { |
| 306 |
self.to_owned() |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
let s: <str as StrExt>::Assoc = "?".owned(); |
| 311 |
assert_eq!(s, "?"); |
| 312 |
|
| 313 |
#[ext(TryIterator)] |
| 314 |
impl<I: Iterator<Item = Result<T, E>>, T, E> I { |
| 315 |
type Ok = T; |
| 316 |
type Error = E; |
| 317 |
|
| 318 |
fn try_next(&mut self) -> Result<Option<Self::Ok>, Self::Error> { |
| 319 |
self.next().transpose() |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
let mut iter = vec![Ok(1), Err(1)].into_iter(); |
| 324 |
assert_eq!(iter.try_next(), Ok(Some(1))); |
| 325 |
assert_eq!(iter.try_next(), Err(1)); |
| 326 |
assert_eq!(iter.try_next(), Ok(None)); |
| 327 |
} |
| 328 |
|
| 329 |
#[allow(clippy::let_underscore_future)] |
| 330 |
#[test] |
| 331 |
fn syntax() { |
| 332 |
// TODO: `unsafe impl` became syntactically invalid in https://github.com/rust-lang/rust/pull/144386#issuecomment-3133213586 |
| 333 |
#[ext(E1)] |
| 334 |
impl str { |
| 335 |
fn normal(&self) {} |
| 336 |
unsafe fn unsafety(&self) {} |
| 337 |
extern "C" fn abi1() {} |
| 338 |
extern "C" fn abi2() {} |
| 339 |
unsafe extern "C" fn unsafe_abi1() {} |
| 340 |
unsafe extern "C" fn unsafe_abi2() {} |
| 341 |
async fn asyncness(&self) {} |
| 342 |
async unsafe fn unsafe_asyncness(&self) {} |
| 343 |
} |
| 344 |
|
| 345 |
"a".normal(); |
| 346 |
unsafe { "?".unsafety() } |
| 347 |
str::abi1(); |
| 348 |
unsafe { str::unsafe_abi1() } |
| 349 |
let _ = async { |
| 350 |
"a".asyncness().await; |
| 351 |
unsafe { "b".unsafe_asyncness().await } |
| 352 |
}; |
| 353 |
|
| 354 |
struct S {} |
| 355 |
impl E1 for S { |
| 356 |
fn normal(&self) {} |
| 357 |
unsafe fn unsafety(&self) {} |
| 358 |
extern "C" fn abi1() {} |
| 359 |
extern "C" fn abi2() {} |
| 360 |
unsafe extern "C" fn unsafe_abi1() {} |
| 361 |
unsafe extern "C" fn unsafe_abi2() {} |
| 362 |
async fn asyncness(&self) {} |
| 363 |
async unsafe fn unsafe_asyncness(&self) {} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// test for angle bracket |
| 368 |
#[test] |
| 369 |
fn angle_bracket() { |
| 370 |
#[ext] |
| 371 |
impl fn() -> () { |
| 372 |
const FUNC: fn() -> fn() -> fn() -> () = Self::func; |
| 373 |
type Func = fn() -> fn() -> (); |
| 374 |
fn func() -> fn() -> fn() -> () { |
| 375 |
|| || {} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
#[ext(E1)] |
| 380 |
impl<T> T |
| 381 |
where |
| 382 |
Self::Assoc3: Sized, |
| 383 |
T::Assoc3: Sized, |
| 384 |
Self: E2, |
| 385 |
T: E2, |
| 386 |
{ |
| 387 |
const ASSOC1: <Self>::Assoc1 = <Self>::assoc1; |
| 388 |
type Assoc1 = fn() -> <Self>::Assoc2; |
| 389 |
type Assoc2 = (); |
| 390 |
fn assoc1() -> <Self>::Assoc2 |
| 391 |
where |
| 392 |
<Self as E1>::Assoc1: Sized, |
| 393 |
<T as E1>::Assoc1: Sized, |
| 394 |
Self::Assoc1: Sized, |
| 395 |
T::Assoc3: Sized, |
| 396 |
Self: E2, |
| 397 |
T: E2, |
| 398 |
{ |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
struct A {} |
| 403 |
#[ext(E2)] |
| 404 |
impl A { |
| 405 |
const ASSOC1: <Self>::Assoc3 = <Self>::assoc2; |
| 406 |
type Assoc3 = fn() -> <Self>::Assoc4; |
| 407 |
type Assoc4 = (); |
| 408 |
fn assoc2() -> <Self>::Assoc4 |
| 409 |
where |
| 410 |
<Self as E2>::Assoc3: Sized, |
| 411 |
Self::Assoc3: Sized, |
| 412 |
{ |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
#[ext] |
| 417 |
impl<T: Fn() -> fn() -> T, E> Result<T, E> |
| 418 |
where |
| 419 |
E: FnOnce() -> Result<T, E>, |
| 420 |
&'static dyn Fn() -> T: Fn() -> T + 'static, |
| 421 |
fn() -> fn() -> T: Fn() -> fn() -> T, |
| 422 |
{ |
| 423 |
fn where_clause<U: Fn() -> fn() -> T, F>(self, _f: F) -> Self |
| 424 |
where |
| 425 |
F: FnOnce() -> Result<U, E>, |
| 426 |
&'static dyn Fn() -> T: Fn() -> T + 'static, |
| 427 |
fn() -> fn() -> T: Fn() -> fn() -> T, |
| 428 |
{ |
| 429 |
unimplemented!() |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn min_const_generics() { |
| 436 |
struct S1<T, const CAP: usize>([T; CAP]); |
| 437 |
#[ext(E1)] |
| 438 |
impl<T, const CAP: usize> S1<T, CAP> { |
| 439 |
const CAPACITY: usize = CAP; |
| 440 |
fn f<const C: usize>() -> S1<T, C> { |
| 441 |
unimplemented!() |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
struct S2<const CAP: usize>; |
| 446 |
impl<const CAP: usize> E1<(), { CAP }> for S2<{ CAP }> { |
| 447 |
const CAPACITY: usize = CAP; |
| 448 |
fn f<const C: usize>() -> S1<(), { C }> { |
| 449 |
S1([(); C]) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
let _: [(); 2] = <S2<3>>::f::<2>().0; |
| 454 |
|
| 455 |
struct S3<T, const CAP: char>(T); |
| 456 |
|
| 457 |
#[ext(E2)] |
| 458 |
impl str { |
| 459 |
fn method1(&self) -> S1<Option<fn() -> ()>, 1> { |
| 460 |
S1([Some(|| {})]) |
| 461 |
} |
| 462 |
#[allow(unused_braces)] |
| 463 |
fn method2(&self) -> S1<Option<fn() -> ()>, { 1 }> { |
| 464 |
S1([Some(|| {})]) |
| 465 |
} |
| 466 |
fn method3(&self) -> S3<fn() -> (), 'a'> { |
| 467 |
S3(|| {}) |
| 468 |
} |
| 469 |
#[allow(unused_braces)] |
| 470 |
fn method4(&self) -> S3<fn() -> (), { 'a' }> { |
| 471 |
S3(|| {}) |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
mod const_generics_defaults { |
| 477 |
// https://github.com/rust-lang/rust/tree/1.80.0/tests/ui/const-generics/defaults |
| 478 |
|
| 479 |
use easy_ext::ext; |
| 480 |
|
| 481 |
#[ext(Ext)] |
| 482 |
impl<const N: usize = 3> () {} |
| 483 |
impl Ext for u8 {} |
| 484 |
|
| 485 |
// The code above is equivalent to the code below. |
| 486 |
|
| 487 |
trait Trait<const N: usize = 3> {} |
| 488 |
impl<const N: usize> Trait<N> for () {} |
| 489 |
impl Trait for u8 {} |
| 490 |
|
| 491 |
// https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/const-generics/defaults/const-param-as-default-value.rs |
| 492 |
#[ext(Ext2)] |
| 493 |
impl<const N: usize, const M: usize = N> () {} |
| 494 |
} |
| 495 |
|
| 496 |
mod generic_associated_types { |
| 497 |
// https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/generic-associated-types/collections.rs |
| 498 |
|
| 499 |
use easy_ext::ext; |
| 500 |
|
| 501 |
trait CollectionFamily { |
| 502 |
type Member<T>: Collection<T, Family = Self>; |
| 503 |
} |
| 504 |
|
| 505 |
struct VecFamily; |
| 506 |
|
| 507 |
impl CollectionFamily for VecFamily { |
| 508 |
type Member<T> = Vec<T>; |
| 509 |
} |
| 510 |
|
| 511 |
#[ext(Collection)] |
| 512 |
impl<T> Vec<T> { |
| 513 |
// TODO: handle where clause in GAT: https://github.com/rust-lang/rust/pull/90076 |
| 514 |
// type Iter<'iter> = std::slice::Iter<'iter, T> |
| 515 |
// where |
| 516 |
// T: 'iter, |
| 517 |
// Self: 'iter; |
| 518 |
type Family = VecFamily; |
| 519 |
type Sibling<U> = <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>; |
| 520 |
|
| 521 |
fn empty() -> Self { |
| 522 |
vec![] |
| 523 |
} |
| 524 |
|
| 525 |
fn add(&mut self, value: T) { |
| 526 |
self.push(value); |
| 527 |
} |
| 528 |
|
| 529 |
// TODO: handle where clause in GAT: https://github.com/rust-lang/rust/pull/90076 |
| 530 |
// fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> { |
| 531 |
// self.iter() |
| 532 |
// } |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
mod associated_type_bounds { |
| 537 |
// https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/associated-type-bounds/fn-where.rs |
| 538 |
mod fn_where { |
| 539 |
use easy_ext::ext; |
| 540 |
|
| 541 |
use super::fn_aux::*; |
| 542 |
|
| 543 |
#[ext(E1)] |
| 544 |
impl<T> T { |
| 545 |
fn where_bound<B>(beta: B) -> usize |
| 546 |
where |
| 547 |
B: Beta<Gamma: Alpha>, |
| 548 |
{ |
| 549 |
desugared_bound(beta) |
| 550 |
} |
| 551 |
|
| 552 |
fn where_bound_region<B>(beta: B) -> usize |
| 553 |
where |
| 554 |
B: Beta<Gamma: 'static>, |
| 555 |
{ |
| 556 |
desugared_bound_region(beta) |
| 557 |
} |
| 558 |
|
| 559 |
fn where_bound_multi<B>(beta: B) -> usize |
| 560 |
where |
| 561 |
B: Copy + Beta<Gamma: Alpha + 'static + Delta>, |
| 562 |
{ |
| 563 |
desugared_bound_multi(beta) |
| 564 |
} |
| 565 |
|
| 566 |
fn where_bound_region_specific<'a, B>(gamma: &'a B::Gamma) -> usize |
| 567 |
where |
| 568 |
B: Beta<Gamma: 'a + Epsilon<'a>>, |
| 569 |
{ |
| 570 |
desugared_bound_region_specific::<B>(gamma) |
| 571 |
} |
| 572 |
|
| 573 |
fn where_bound_region_forall<B>(beta: B) -> usize |
| 574 |
where |
| 575 |
B: Beta<Gamma: Copy + for<'a> Epsilon<'a>>, |
| 576 |
{ |
| 577 |
desugared_bound_region_forall(beta) |
| 578 |
} |
| 579 |
|
| 580 |
fn where_bound_region_forall2<B>(beta: B) -> usize |
| 581 |
where |
| 582 |
B: Beta<Gamma: Copy + for<'a> Epsilon<'a, Zeta: Eta>>, |
| 583 |
{ |
| 584 |
desugared_bound_region_forall2(beta) |
| 585 |
} |
| 586 |
|
| 587 |
fn where_constraint_region_forall<B>(beta: B) -> usize |
| 588 |
where |
| 589 |
for<'a> &'a B: Beta<Gamma: Alpha>, |
| 590 |
{ |
| 591 |
desugared_constraint_region_forall(beta) |
| 592 |
} |
| 593 |
|
| 594 |
fn where_bound_nested<B>(beta: B) -> usize |
| 595 |
where |
| 596 |
B: Beta<Gamma: Copy + Alpha + Beta<Gamma: Delta>>, |
| 597 |
{ |
| 598 |
desugared_bound_nested(beta) |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
// https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/associated-type-bounds/auxiliary/fn-aux.rs |
| 604 |
mod fn_aux { |
| 605 |
// Traits: |
| 606 |
|
| 607 |
pub trait Alpha { |
| 608 |
fn alpha(self) -> usize; |
| 609 |
} |
| 610 |
|
| 611 |
pub trait Beta { |
| 612 |
type Gamma; |
| 613 |
fn gamma(self) -> Self::Gamma; |
| 614 |
} |
| 615 |
|
| 616 |
pub trait Delta { |
| 617 |
fn delta(self) -> usize; |
| 618 |
} |
| 619 |
|
| 620 |
pub trait Epsilon<'a> { |
| 621 |
type Zeta; |
| 622 |
fn zeta(&'a self) -> Self::Zeta; |
| 623 |
|
| 624 |
fn epsilon(&'a self) -> usize; |
| 625 |
} |
| 626 |
|
| 627 |
pub trait Eta { |
| 628 |
fn eta(self) -> usize; |
| 629 |
} |
| 630 |
|
| 631 |
// Assertions: |
| 632 |
|
| 633 |
pub fn assert_alpha<T: Alpha>(x: T) -> usize { |
| 634 |
x.alpha() |
| 635 |
} |
| 636 |
pub fn assert_static<T: 'static>(_: T) -> usize { |
| 637 |
24 |
| 638 |
} |
| 639 |
pub fn assert_delta<T: Delta>(x: T) -> usize { |
| 640 |
x.delta() |
| 641 |
} |
| 642 |
pub fn assert_epsilon_specific<'a, T: 'a + Epsilon<'a>>(x: &'a T) -> usize { |
| 643 |
x.epsilon() |
| 644 |
} |
| 645 |
pub fn assert_epsilon_forall<T: for<'a> Epsilon<'a>>() {} |
| 646 |
pub fn assert_forall_epsilon_zeta_satisfies_eta<T>(x: T) -> usize |
| 647 |
where |
| 648 |
T: for<'a> Epsilon<'a>, |
| 649 |
for<'a> <T as Epsilon<'a>>::Zeta: Eta, |
| 650 |
{ |
| 651 |
x.epsilon() + x.zeta().eta() |
| 652 |
} |
| 653 |
|
| 654 |
// Implementations and types: |
| 655 |
|
| 656 |
#[derive(Clone, Copy)] |
| 657 |
pub struct BetaType; |
| 658 |
|
| 659 |
#[derive(Clone, Copy)] |
| 660 |
pub struct GammaType; |
| 661 |
|
| 662 |
#[derive(Clone, Copy)] |
| 663 |
pub struct ZetaType; |
| 664 |
|
| 665 |
impl Beta for BetaType { |
| 666 |
type Gamma = GammaType; |
| 667 |
fn gamma(self) -> Self::Gamma { |
| 668 |
GammaType |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
impl Beta for &BetaType { |
| 673 |
type Gamma = GammaType; |
| 674 |
fn gamma(self) -> Self::Gamma { |
| 675 |
GammaType |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
impl Beta for GammaType { |
| 680 |
type Gamma = Self; |
| 681 |
fn gamma(self) -> Self::Gamma { |
| 682 |
self |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
impl Alpha for GammaType { |
| 687 |
fn alpha(self) -> usize { |
| 688 |
42 |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
impl Delta for GammaType { |
| 693 |
fn delta(self) -> usize { |
| 694 |
1337 |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
impl<'a> Epsilon<'a> for GammaType { |
| 699 |
type Zeta = ZetaType; |
| 700 |
fn zeta(&'a self) -> Self::Zeta { |
| 701 |
ZetaType |
| 702 |
} |
| 703 |
|
| 704 |
fn epsilon(&'a self) -> usize { |
| 705 |
7331 |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
impl Eta for ZetaType { |
| 710 |
fn eta(self) -> usize { |
| 711 |
7 |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
// Desugared forms to check against: |
| 716 |
|
| 717 |
pub fn desugared_bound<B>(beta: B) -> usize |
| 718 |
where |
| 719 |
B: Beta, |
| 720 |
B::Gamma: Alpha, |
| 721 |
{ |
| 722 |
let gamma: B::Gamma = beta.gamma(); |
| 723 |
assert_alpha::<B::Gamma>(gamma) |
| 724 |
} |
| 725 |
|
| 726 |
pub fn desugared_bound_region<B>(beta: B) -> usize |
| 727 |
where |
| 728 |
B: Beta, |
| 729 |
B::Gamma: 'static, |
| 730 |
{ |
| 731 |
assert_static::<B::Gamma>(beta.gamma()) |
| 732 |
} |
| 733 |
|
| 734 |
pub fn desugared_bound_multi<B>(beta: B) -> usize |
| 735 |
where |
| 736 |
B: Copy + Beta, |
| 737 |
B::Gamma: Alpha + 'static + Delta, |
| 738 |
{ |
| 739 |
assert_alpha::<B::Gamma>(beta.gamma()) |
| 740 |
+ assert_static::<B::Gamma>(beta.gamma()) |
| 741 |
+ assert_delta::<B::Gamma>(beta.gamma()) |
| 742 |
} |
| 743 |
|
| 744 |
pub fn desugared_bound_region_specific<'a, B>(gamma: &'a B::Gamma) -> usize |
| 745 |
where |
| 746 |
B: Beta, |
| 747 |
B::Gamma: 'a + Epsilon<'a>, |
| 748 |
{ |
| 749 |
assert_epsilon_specific::<B::Gamma>(gamma) |
| 750 |
} |
| 751 |
|
| 752 |
pub fn desugared_bound_region_forall<B>(beta: B) -> usize |
| 753 |
where |
| 754 |
B: Beta, |
| 755 |
B::Gamma: Copy + for<'a> Epsilon<'a>, |
| 756 |
{ |
| 757 |
assert_epsilon_forall::<B::Gamma>(); |
| 758 |
let g1: B::Gamma = beta.gamma(); |
| 759 |
let g2: B::Gamma = g1; |
| 760 |
assert_epsilon_specific::<B::Gamma>(&g1) + assert_epsilon_specific::<B::Gamma>(&g2) |
| 761 |
} |
| 762 |
|
| 763 |
pub fn desugared_bound_region_forall2<B>(beta: B) -> usize |
| 764 |
where |
| 765 |
B: Beta, |
| 766 |
B::Gamma: Copy + for<'a> Epsilon<'a>, |
| 767 |
for<'a> <B::Gamma as Epsilon<'a>>::Zeta: Eta, |
| 768 |
{ |
| 769 |
let gamma = beta.gamma(); |
| 770 |
assert_forall_epsilon_zeta_satisfies_eta::<B::Gamma>(gamma) |
| 771 |
} |
| 772 |
|
| 773 |
pub fn desugared_constraint_region_forall<B>(beta: B) -> usize |
| 774 |
where |
| 775 |
for<'a> &'a B: Beta, |
| 776 |
for<'a> <&'a B as Beta>::Gamma: Alpha, |
| 777 |
{ |
| 778 |
let g1 = beta.gamma(); |
| 779 |
let g2 = beta.gamma(); |
| 780 |
assert_alpha(g1) + assert_alpha(g2) |
| 781 |
} |
| 782 |
|
| 783 |
pub fn desugared_bound_nested<B>(beta: B) -> usize |
| 784 |
where |
| 785 |
B: Beta, |
| 786 |
B::Gamma: Copy + Alpha + Beta, |
| 787 |
<B::Gamma as Beta>::Gamma: Delta, |
| 788 |
{ |
| 789 |
let go = beta.gamma(); |
| 790 |
let gi = go.gamma(); |
| 791 |
go.alpha() + gi.delta() |
| 792 |
} |
| 793 |
|
| 794 |
pub fn desugared() { |
| 795 |
let beta = BetaType; |
| 796 |
let gamma = beta.gamma(); |
| 797 |
|
| 798 |
assert_eq!(42, desugared_bound(beta)); |
| 799 |
assert_eq!(24, desugared_bound_region(beta)); |
| 800 |
assert_eq!(42 + 24 + 1337, desugared_bound_multi(beta)); |
| 801 |
assert_eq!(7331, desugared_bound_region_specific::<BetaType>(&gamma)); |
| 802 |
assert_eq!(7331 * 2, desugared_bound_region_forall(beta)); |
| 803 |
assert_eq!(42 + 1337, desugared_bound_nested(beta)); |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn macros() { |
| 810 |
macro_rules! m { |
| 811 |
( |
| 812 |
$impl:ident $path:path [$($generics:tt)*] $where:ident {$( |
| 813 |
[$vis:vis, $($fn_sig:ident)*] |
| 814 |
)*} |
| 815 |
) => { |
| 816 |
$( |
| 817 |
#[ext] |
| 818 |
$impl<T, E> Result<T, E> { |
| 819 |
$vis $($fn_sig)* <U>(self) -> Result<T, U> |
| 820 |
where |
| 821 |
E: Into<U>, |
| 822 |
{ |
| 823 |
unimplemented!() |
| 824 |
} |
| 825 |
} |
| 826 |
)* |
| 827 |
}; |
| 828 |
} |
| 829 |
|
| 830 |
m!(impl Result [T,E] where { |
| 831 |
[, fn a] |
| 832 |
[pub, fn b] |
| 833 |
[pub, unsafe fn c] |
| 834 |
[pub(crate), fn d] |
| 835 |
}); |
| 836 |
} |
| 837 |
|
| 838 |
// https://github.com/taiki-e/easy-ext/issues/36 |
| 839 |
#[test] |
| 840 |
fn where_clause() { |
| 841 |
pub trait Trait<T> {} |
| 842 |
|
| 843 |
#[rustfmt::skip] |
| 844 |
#[ext] |
| 845 |
pub impl<T> Vec<T> |
| 846 |
where |
| 847 |
Self: Trait<Vec<T>> |
| 848 |
{ |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
#[allow(clippy::needless_pub_self)] // This is intentional |
| 853 |
pub mod visibility { |
| 854 |
use easy_ext::ext; |
| 855 |
|
| 856 |
pub struct Pub; |
| 857 |
#[ext] |
| 858 |
impl str { |
| 859 |
pub const ASSOC: u8 = 1; |
| 860 |
pub type Assoc = u8; |
| 861 |
pub fn assoc() {} |
| 862 |
} |
| 863 |
|
| 864 |
pub struct PubCrate; |
| 865 |
#[ext] |
| 866 |
impl PubCrate { |
| 867 |
pub(crate) const ASSOC: u8 = 1; |
| 868 |
pub(crate) type Assoc = u8; |
| 869 |
pub(crate) fn assoc() {} |
| 870 |
} |
| 871 |
|
| 872 |
pub struct PubSelf; |
| 873 |
#[ext] |
| 874 |
impl PubSelf { |
| 875 |
pub(self) const ASSOC: u8 = 1; |
| 876 |
pub(self) type Assoc = u8; |
| 877 |
pub(self) fn assoc() {} |
| 878 |
} |
| 879 |
|
| 880 |
pub mod m { |
| 881 |
use easy_ext::ext; |
| 882 |
|
| 883 |
pub struct PubSuper; |
| 884 |
#[ext] |
| 885 |
impl PubSuper { |
| 886 |
pub(super) const ASSOC: u8 = 1; |
| 887 |
pub(super) type Assoc = u8; |
| 888 |
pub(super) fn assoc() {} |
| 889 |
} |
| 890 |
|
| 891 |
pub struct PubIn; |
| 892 |
#[ext] |
| 893 |
impl PubIn { |
| 894 |
pub(in super::m) const ASSOC: u8 = 1; |
| 895 |
pub(in super::m) type Assoc = u8; |
| 896 |
pub(in super::m) fn assoc() {} |
| 897 |
} |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn arg_pat() { |
| 903 |
#[ext] |
| 904 |
impl str { |
| 905 |
fn f((x, y): (u8, u8)) { |
| 906 |
let _x = x; |
| 907 |
let _y = y; |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
#[test] |
| 913 |
fn arbitrary_self_types() { |
| 914 |
#[ext] |
| 915 |
#[allow(clippy::needless_arbitrary_self_type)] |
| 916 |
impl String { |
| 917 |
fn recv(self: Self) {} |
| 918 |
fn recv_ref(self: &Self) {} |
| 919 |
fn recv_mut(self: &mut Self) {} |
| 920 |
fn recv_rc(self: Rc<Self>) {} |
| 921 |
fn recv_rc_ref(self: &Rc<Self>) {} |
| 922 |
fn recv_rc_mut(self: &mut Rc<Self>) {} |
| 923 |
fn recv_pin_box(self: Pin<Box<Self>>) {} |
| 924 |
} |
| 925 |
|
| 926 |
String::default().recv(); |
| 927 |
String::default().recv_ref(); |
| 928 |
String::default().recv_mut(); |
| 929 |
Rc::new(String::default()).recv_rc(); |
| 930 |
Rc::new(String::default()).recv_rc_ref(); |
| 931 |
Rc::new(String::default()).recv_rc_mut(); |
| 932 |
Box::pin(String::default()).recv_pin_box(); |
| 933 |
} |