Skip to content

Commit d026479

Browse files
committed
Support #[rustc_coinductive]
1 parent 6ba2590 commit d026479

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

crates/hir-ty/src/chalk_db.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,18 @@ pub(crate) fn trait_datum_query(
550550
debug!("trait_datum {:?}", trait_id);
551551
let trait_ = from_chalk_trait_id(trait_id);
552552
let trait_data = db.trait_data(trait_);
553+
554+
let coinductive =
555+
trait_data.is_auto || db.attrs(trait_.into()).by_key("rustc_coinductive").exists();
556+
553557
debug!("trait {:?} = {:?}", trait_id, trait_data.name);
554558
let generic_params = generics(db.upcast(), trait_.into());
555559
let bound_vars = generic_params.bound_vars_subst(db, DebruijnIndex::INNERMOST);
556560
let flags = rust_ir::TraitFlags {
557561
auto: trait_data.is_auto,
558562
upstream: trait_.lookup(db.upcast()).container.krate() != krate,
559563
non_enumerable: true,
560-
coinductive: false, // only relevant for Chalk testing
564+
coinductive,
561565
// FIXME: set these flags correctly
562566
marker: false,
563567
fundamental: false,
@@ -637,7 +641,7 @@ pub(crate) fn struct_datum_query(
637641
fundamental: false,
638642
phantom_data: false,
639643
};
640-
// FIXME provide enum variants properly (for auto traits)
644+
// FIXME provide enum variants properly (for auto traits and `Sized`)
641645
let variant = rust_ir::AdtVariantDatum {
642646
fields: Vec::new(), // FIXME add fields (only relevant for auto traits),
643647
};

crates/hir-ty/src/tests/traits.rs

+32
Original file line numberDiff line numberDiff line change
@@ -4410,3 +4410,35 @@ fn test(v: S<i32>) {
44104410
"#,
44114411
);
44124412
}
4413+
4414+
#[test]
4415+
fn rustc_coinductive() {
4416+
// Taken from rust-lang/rust#108033 with modification.
4417+
check_types(
4418+
r#"
4419+
#[rustc_coinductive]
4420+
trait Trait { type Assoc; }
4421+
4422+
impl<T, U> Trait for (T, U)
4423+
where
4424+
(U, T): Trait,
4425+
(): ConstrainToU32<T>,
4426+
{
4427+
type Assoc = i32;
4428+
}
4429+
4430+
trait ConstrainToU32<T> {}
4431+
impl ConstrainToU32<u32> for () {}
4432+
4433+
fn impls_trait<T, U, R>() -> R
4434+
where
4435+
(T, U): Trait<Assoc = R>,
4436+
{ loop {} }
4437+
4438+
fn main() {
4439+
let _ = impls_trait::<_, _, _>();
4440+
//^ i32
4441+
}
4442+
"#,
4443+
);
4444+
}

crates/ide/src/inlay_hints/chaining.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fn main() {
474474
file_id: FileId(
475475
1,
476476
),
477-
range: 9288..9296,
477+
range: 9313..9321,
478478
},
479479
),
480480
tooltip: "",
@@ -487,7 +487,7 @@ fn main() {
487487
file_id: FileId(
488488
1,
489489
),
490-
range: 9320..9324,
490+
range: 9345..9349,
491491
},
492492
),
493493
tooltip: "",
@@ -511,7 +511,7 @@ fn main() {
511511
file_id: FileId(
512512
1,
513513
),
514-
range: 9288..9296,
514+
range: 9313..9321,
515515
},
516516
),
517517
tooltip: "",
@@ -524,7 +524,7 @@ fn main() {
524524
file_id: FileId(
525525
1,
526526
),
527-
range: 9320..9324,
527+
range: 9345..9349,
528528
},
529529
),
530530
tooltip: "",
@@ -548,7 +548,7 @@ fn main() {
548548
file_id: FileId(
549549
1,
550550
),
551-
range: 9288..9296,
551+
range: 9313..9321,
552552
},
553553
),
554554
tooltip: "",
@@ -561,7 +561,7 @@ fn main() {
561561
file_id: FileId(
562562
1,
563563
),
564-
range: 9320..9324,
564+
range: 9345..9349,
565565
},
566566
),
567567
tooltip: "",

crates/test-utils/src/minicore.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub mod marker {
6262
#[lang = "sized"]
6363
#[fundamental]
6464
#[rustc_specialization_trait]
65+
#[rustc_coinductive]
6566
pub trait Sized {}
6667
// endregion:sized
6768

0 commit comments

Comments
 (0)