Skip to content

Commit 847c552

Browse files
Merge #11936
11936: Ignore `Drop` and `Destruct` bounds for now r=flodiebold a=flodiebold - `T: ~const Drop` has a special meaning in Rust 1.61 that we don't implement. (So ideally, we'd only ignore `~const Drop`, but this should be fine for now.) - `Destruct` impls are built-in in 1.62 (current nightlies), so until the builtin impls are supported by Chalk, we ignore them as well. Since `Destruct` is implemented for everything in non-const contexts IIUC, this should also work fine. Fixes #11932. Co-authored-by: Florian Diebold <[email protected]>
2 parents bc56920 + 340f0fc commit 847c552

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

crates/hir_ty/src/lower.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use chalk_ir::interner::HasInterner;
1414
use chalk_ir::{cast::Cast, fold::Shift, Mutability, Safety};
1515
use hir_def::generics::TypeOrConstParamData;
1616
use hir_def::intern::Interned;
17+
use hir_def::lang_item::lang_attr;
1718
use hir_def::path::{ModPath, PathKind};
1819
use hir_def::type_ref::ConstScalarOrPath;
1920
use hir_def::{
@@ -863,7 +864,23 @@ impl<'a> TyLoweringContext<'a> {
863864
let trait_ref = match bound {
864865
TypeBound::Path(path, TraitBoundModifier::None) => {
865866
bindings = self.lower_trait_ref_from_path(path, Some(self_ty));
866-
bindings.clone().map(WhereClause::Implemented).map(crate::wrap_empty_binders)
867+
bindings
868+
.clone()
869+
.filter(|tr| {
870+
// ignore `T: Drop` or `T: Destruct` bounds.
871+
// - `T: ~const Drop` has a special meaning in Rust 1.61 that we don't implement.
872+
// (So ideally, we'd only ignore `~const Drop` here)
873+
// - `Destruct` impls are built-in in 1.62 (current nightlies as of 08-04-2022), so until
874+
// the builtin impls are supported by Chalk, we ignore them here.
875+
if let Some(lang) = lang_attr(self.db.upcast(), tr.hir_trait_id()) {
876+
if lang == "drop" || lang == "destruct" {
877+
return false;
878+
}
879+
}
880+
true
881+
})
882+
.map(WhereClause::Implemented)
883+
.map(crate::wrap_empty_binders)
867884
}
868885
TypeBound::Path(path, TraitBoundModifier::Maybe) => {
869886
let sized_trait = self

crates/hir_ty/src/tests/regression.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,82 @@ fn f(s: S) {
15051505
"#,
15061506
);
15071507
}
1508+
1509+
#[test]
1510+
fn rust_161_option_clone() {
1511+
check_types(
1512+
r#"
1513+
//- minicore: option, drop
1514+
1515+
fn test(o: &Option<i32>) {
1516+
o.my_clone();
1517+
//^^^^^^^^^^^^ Option<i32>
1518+
}
1519+
1520+
pub trait MyClone: Sized {
1521+
fn my_clone(&self) -> Self;
1522+
}
1523+
1524+
impl<T> const MyClone for Option<T>
1525+
where
1526+
T: ~const MyClone + ~const Drop + ~const Destruct,
1527+
{
1528+
fn my_clone(&self) -> Self {
1529+
match self {
1530+
Some(x) => Some(x.my_clone()),
1531+
None => None,
1532+
}
1533+
}
1534+
}
1535+
1536+
impl const MyClone for i32 {
1537+
fn my_clone(&self) -> Self {
1538+
*self
1539+
}
1540+
}
1541+
1542+
pub trait Destruct {}
1543+
1544+
impl<T: ?Sized> const Destruct for T {}
1545+
"#,
1546+
);
1547+
}
1548+
1549+
#[test]
1550+
fn rust_162_option_clone() {
1551+
check_types(
1552+
r#"
1553+
//- minicore: option, drop
1554+
1555+
fn test(o: &Option<i32>) {
1556+
o.my_clone();
1557+
//^^^^^^^^^^^^ Option<i32>
1558+
}
1559+
1560+
pub trait MyClone: Sized {
1561+
fn my_clone(&self) -> Self;
1562+
}
1563+
1564+
impl<T> const MyClone for Option<T>
1565+
where
1566+
T: ~const MyClone + ~const Destruct,
1567+
{
1568+
fn my_clone(&self) -> Self {
1569+
match self {
1570+
Some(x) => Some(x.my_clone()),
1571+
None => None,
1572+
}
1573+
}
1574+
}
1575+
1576+
impl const MyClone for i32 {
1577+
fn my_clone(&self) -> Self {
1578+
*self
1579+
}
1580+
}
1581+
1582+
#[lang = "destruct"]
1583+
pub trait Destruct {}
1584+
"#,
1585+
);
1586+
}

0 commit comments

Comments
 (0)