Rename object_safety to dyn_compatibility

Up to a trait implemented by another package, linking to
$CARGO_HOME/registry/cache/index.crates.io-6f17d22bba15001f/
This commit is contained in:
Noah Bright 2024-09-28 07:01:32 -04:00
parent 01aaa53ef2
commit 4255cae1bb
12 changed files with 112 additions and 106 deletions

View file

@ -382,8 +382,9 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
}
fn is_object_safe(&self, trait_id: chalk_ir::TraitId<Interner>) -> bool {
// FIXME: When cargo is updated, change to dyn_compatibility
let trait_ = from_chalk_trait_id(trait_id);
crate::object_safety::object_safety(self.db, trait_).is_none()
crate::dyn_compatibility::dyn_compatibility(self.db, trait_).is_none()
}
fn closure_kind(

View file

@ -20,11 +20,11 @@ use triomphe::Arc;
use crate::{
chalk_db,
consteval::ConstEvalError,
dyn_compatibility::DynCompatibilityViolation,
layout::{Layout, LayoutError},
lower::{GenericDefaults, GenericPredicates},
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
mir::{BorrowckResult, MirBody, MirLowerError},
object_safety::ObjectSafetyViolation,
Binders, ClosureId, Const, FnDefId, ImplTraitId, ImplTraits, InferenceResult, Interner,
PolyFnSig, Substitution, TraitEnvironment, TraitRef, Ty, TyDefId, ValueTyDefId,
};
@ -108,8 +108,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Result<Arc<TargetDataLayout>, Arc<str>>;
#[salsa::invoke(crate::object_safety::object_safety_of_trait_query)]
fn object_safety_of_trait(&self, trait_: TraitId) -> Option<ObjectSafetyViolation>;
#[salsa::invoke(crate::dyn_compatibility::dyn_compatibility_of_trait_query)]
fn dyn_compatibility_of_trait(&self, trait_: TraitId) -> Option<DynCompatibilityViolation>;
#[salsa::invoke(crate::lower::ty_query)]
#[salsa::cycle(crate::lower::ty_recover)]
@ -280,8 +280,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
}
#[test]
fn hir_database_is_object_safe() {
fn _assert_object_safe(_: &dyn HirDatabase) {}
fn hir_database_is_dyn_compatible() {
fn _assert_dyn_compatible(_: &dyn HirDatabase) {}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

View file

@ -1,4 +1,4 @@
//! Compute the object-safety of a trait
//! Compute the dyn-compatibility of a trait
use std::ops::ControlFlow;
@ -28,14 +28,14 @@ use crate::{
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ObjectSafetyViolation {
pub enum DynCompatibilityViolation {
SizedSelf,
SelfReferential,
Method(FunctionId, MethodViolationCode),
AssocConst(ConstId),
GAT(TypeAliasId),
// This doesn't exist in rustc, but added for better visualization
HasNonSafeSuperTrait(TraitId),
HasNonCompatibleSuperTrait(TraitId),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -50,70 +50,73 @@ pub enum MethodViolationCode {
UndispatchableReceiver,
}
pub fn object_safety(db: &dyn HirDatabase, trait_: TraitId) -> Option<ObjectSafetyViolation> {
pub fn dyn_compatibility(
db: &dyn HirDatabase,
trait_: TraitId,
) -> Option<DynCompatibilityViolation> {
for super_trait in all_super_traits(db.upcast(), trait_).into_iter().skip(1).rev() {
if db.object_safety_of_trait(super_trait).is_some() {
return Some(ObjectSafetyViolation::HasNonSafeSuperTrait(super_trait));
if db.dyn_compatibility_of_trait(super_trait).is_some() {
return Some(DynCompatibilityViolation::HasNonCompatibleSuperTrait(super_trait));
}
}
db.object_safety_of_trait(trait_)
db.dyn_compatibility_of_trait(trait_)
}
pub fn object_safety_with_callback<F>(
pub fn dyn_compatibility_with_callback<F>(
db: &dyn HirDatabase,
trait_: TraitId,
cb: &mut F,
) -> ControlFlow<()>
where
F: FnMut(ObjectSafetyViolation) -> ControlFlow<()>,
F: FnMut(DynCompatibilityViolation) -> ControlFlow<()>,
{
for super_trait in all_super_traits(db.upcast(), trait_).into_iter().skip(1).rev() {
if db.object_safety_of_trait(super_trait).is_some() {
cb(ObjectSafetyViolation::HasNonSafeSuperTrait(trait_))?;
if db.dyn_compatibility_of_trait(super_trait).is_some() {
cb(DynCompatibilityViolation::HasNonCompatibleSuperTrait(trait_))?;
}
}
object_safety_of_trait_with_callback(db, trait_, cb)
dyn_compatibility_of_trait_with_callback(db, trait_, cb)
}
pub fn object_safety_of_trait_with_callback<F>(
pub fn dyn_compatibility_of_trait_with_callback<F>(
db: &dyn HirDatabase,
trait_: TraitId,
cb: &mut F,
) -> ControlFlow<()>
where
F: FnMut(ObjectSafetyViolation) -> ControlFlow<()>,
F: FnMut(DynCompatibilityViolation) -> ControlFlow<()>,
{
// Check whether this has a `Sized` bound
if generics_require_sized_self(db, trait_.into()) {
cb(ObjectSafetyViolation::SizedSelf)?;
cb(DynCompatibilityViolation::SizedSelf)?;
}
// Check if there exist bounds that referencing self
if predicates_reference_self(db, trait_) {
cb(ObjectSafetyViolation::SelfReferential)?;
cb(DynCompatibilityViolation::SelfReferential)?;
}
if bounds_reference_self(db, trait_) {
cb(ObjectSafetyViolation::SelfReferential)?;
cb(DynCompatibilityViolation::SelfReferential)?;
}
// rustc checks for non-lifetime binders here, but we don't support HRTB yet
let trait_data = db.trait_data(trait_);
for (_, assoc_item) in &trait_data.items {
object_safety_violation_for_assoc_item(db, trait_, *assoc_item, cb)?;
dyn_compatibility_violation_for_assoc_item(db, trait_, *assoc_item, cb)?;
}
ControlFlow::Continue(())
}
pub fn object_safety_of_trait_query(
pub fn dyn_compatibility_of_trait_query(
db: &dyn HirDatabase,
trait_: TraitId,
) -> Option<ObjectSafetyViolation> {
) -> Option<DynCompatibilityViolation> {
let mut res = None;
object_safety_of_trait_with_callback(db, trait_, &mut |osv| {
dyn_compatibility_of_trait_with_callback(db, trait_, &mut |osv| {
res = Some(osv);
ControlFlow::Break(())
});
@ -321,14 +324,14 @@ fn contains_illegal_self_type_reference<T: TypeVisitable<Interner>>(
t.visit_with(visitor.as_dyn(), outer_binder).is_break()
}
fn object_safety_violation_for_assoc_item<F>(
fn dyn_compatibility_violation_for_assoc_item<F>(
db: &dyn HirDatabase,
trait_: TraitId,
item: AssocItemId,
cb: &mut F,
) -> ControlFlow<()>
where
F: FnMut(ObjectSafetyViolation) -> ControlFlow<()>,
F: FnMut(DynCompatibilityViolation) -> ControlFlow<()>,
{
// Any item that has a `Self : Sized` requisite is otherwise
// exempt from the regulations.
@ -337,10 +340,10 @@ where
}
match item {
AssocItemId::ConstId(it) => cb(ObjectSafetyViolation::AssocConst(it)),
AssocItemId::ConstId(it) => cb(DynCompatibilityViolation::AssocConst(it)),
AssocItemId::FunctionId(it) => {
virtual_call_violations_for_method(db, trait_, it, &mut |mvc| {
cb(ObjectSafetyViolation::Method(it, mvc))
cb(DynCompatibilityViolation::Method(it, mvc))
})
}
AssocItemId::TypeAliasId(it) => {
@ -350,7 +353,7 @@ where
} else {
let generic_params = db.generic_params(item.into());
if !generic_params.is_empty() {
cb(ObjectSafetyViolation::GAT(it))
cb(DynCompatibilityViolation::GAT(it))
} else {
ControlFlow::Continue(())
}
@ -469,7 +472,7 @@ fn receiver_is_dispatchable(
return false;
};
// `self: Self` can't be dispatched on, but this is already considered object safe.
// `self: Self` can't be dispatched on, but this is already considered dyn compatible
// See rustc's comment on https://github.com/rust-lang/rust/blob/3f121b9461cce02a703a0e7e450568849dfaa074/compiler/rustc_trait_selection/src/traits/object_safety.rs#L433-L437
if sig
.skip_binders()

View file

@ -5,29 +5,29 @@ use rustc_hash::{FxHashMap, FxHashSet};
use syntax::ToSmolStr;
use test_fixture::WithFixture;
use crate::{object_safety::object_safety_with_callback, test_db::TestDB};
use crate::{dyn_compatibility::dyn_compatibility_with_callback, test_db::TestDB};
use super::{
DynCompatibilityViolation,
MethodViolationCode::{self, *},
ObjectSafetyViolation,
};
use ObjectSafetyViolationKind::*;
use DynCompatibilityViolationKind::*;
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ObjectSafetyViolationKind {
enum DynCompatibilityViolationKind {
SizedSelf,
SelfReferential,
Method(MethodViolationCode),
AssocConst,
GAT,
HasNonSafeSuperTrait,
HasNonCompatibleSuperTrait,
}
fn check_object_safety<'a>(
fn check_dyn_compatibility<'a>(
ra_fixture: &str,
expected: impl IntoIterator<Item = (&'a str, Vec<ObjectSafetyViolationKind>)>,
expected: impl IntoIterator<Item = (&'a str, Vec<DynCompatibilityViolationKind>)>,
) {
let mut expected: FxHashMap<_, _> =
expected.into_iter().map(|(id, osvs)| (id, FxHashSet::from_iter(osvs))).collect();
@ -53,18 +53,20 @@ fn check_object_safety<'a>(
continue;
};
let mut osvs = FxHashSet::default();
object_safety_with_callback(&db, trait_id, &mut |osv| {
dyn_compatibility_with_callback(&db, trait_id, &mut |osv| {
osvs.insert(match osv {
ObjectSafetyViolation::SizedSelf => SizedSelf,
ObjectSafetyViolation::SelfReferential => SelfReferential,
ObjectSafetyViolation::Method(_, mvc) => Method(mvc),
ObjectSafetyViolation::AssocConst(_) => AssocConst,
ObjectSafetyViolation::GAT(_) => GAT,
ObjectSafetyViolation::HasNonSafeSuperTrait(_) => HasNonSafeSuperTrait,
DynCompatibilityViolation::SizedSelf => SizedSelf,
DynCompatibilityViolation::SelfReferential => SelfReferential,
DynCompatibilityViolation::Method(_, mvc) => Method(mvc),
DynCompatibilityViolation::AssocConst(_) => AssocConst,
DynCompatibilityViolation::GAT(_) => GAT,
DynCompatibilityViolation::HasNonCompatibleSuperTrait(_) => {
HasNonCompatibleSuperTrait
}
});
ControlFlow::Continue(())
});
assert_eq!(osvs, expected, "Object safety violations for `{name}` do not match;");
assert_eq!(osvs, expected, "Dyn Compatibility violations for `{name}` do not match;");
}
let remains: Vec<_> = expected.keys().collect();
@ -73,7 +75,7 @@ fn check_object_safety<'a>(
#[test]
fn item_bounds_can_reference_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: eq
pub trait Foo {
@ -88,7 +90,7 @@ pub trait Foo {
#[test]
fn associated_consts() {
check_object_safety(
check_dyn_compatibility(
r#"
trait Bar {
const X: usize;
@ -100,7 +102,7 @@ trait Bar {
#[test]
fn bounds_reference_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: eq
trait X {
@ -113,7 +115,7 @@ trait X {
#[test]
fn by_value_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar {
@ -135,7 +137,7 @@ trait Quux {
#[test]
fn generic_methods() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar {
@ -157,7 +159,7 @@ trait Qax {
#[test]
fn mentions_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar {
@ -182,7 +184,7 @@ trait Quux {
#[test]
fn no_static() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Foo {
@ -195,7 +197,7 @@ trait Foo {
#[test]
fn sized_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar: Sized {
@ -205,7 +207,7 @@ trait Bar: Sized {
[("Bar", vec![SizedSelf])],
);
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar
@ -220,7 +222,7 @@ trait Bar
#[test]
fn supertrait_gat() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait GatTrait {
@ -229,13 +231,13 @@ trait GatTrait {
trait SuperTrait<T>: GatTrait {}
"#,
[("GatTrait", vec![GAT]), ("SuperTrait", vec![HasNonSafeSuperTrait])],
[("GatTrait", vec![GAT]), ("SuperTrait", vec![HasNonCompatibleSuperTrait])],
);
}
#[test]
fn supertrait_mentions_self() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Bar<T> {
@ -251,7 +253,7 @@ trait Baz : Bar<Self> {
#[test]
fn rustc_issue_19538() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Foo {
@ -260,13 +262,13 @@ trait Foo {
trait Bar: Foo {}
"#,
[("Foo", vec![Method(Generic)]), ("Bar", vec![HasNonSafeSuperTrait])],
[("Foo", vec![Method(Generic)]), ("Bar", vec![HasNonCompatibleSuperTrait])],
);
}
#[test]
fn rustc_issue_22040() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: fmt, eq, dispatch_from_dyn
use core::fmt::Debug;
@ -281,7 +283,7 @@ trait Expr: Debug + PartialEq {
#[test]
fn rustc_issue_102762() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: future, send, sync, dispatch_from_dyn, deref
use core::pin::Pin;
@ -313,7 +315,7 @@ pub trait Fetcher: Send + Sync {
#[test]
fn rustc_issue_102933() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: future, dispatch_from_dyn, deref
use core::future::Future;
@ -351,7 +353,7 @@ pub trait B2: Service<Response = i32> + B1 {
#[test]
fn rustc_issue_106247() {
check_object_safety(
check_dyn_compatibility(
r#"
//- minicore: sync, dispatch_from_dyn
pub trait Trait {
@ -363,8 +365,8 @@ pub trait Trait {
}
#[test]
fn std_error_is_object_safe() {
check_object_safety(
fn std_error_is_dyn_compatible() {
check_dyn_compatibility(
r#"
//- minicore: fmt, dispatch_from_dyn
trait Erased<'a>: 'a {}
@ -380,14 +382,14 @@ pub trait Error: core::fmt::Debug + core::fmt::Display {
}
#[test]
fn lifetime_gat_is_object_unsafe() {
check_object_safety(
fn lifetime_gat_is_dyn_incompatible() {
check_dyn_compatibility(
r#"
//- minicore: dispatch_from_dyn
trait Foo {
type Bar<'a>;
}
"#,
[("Foo", vec![ObjectSafetyViolationKind::GAT])],
[("Foo", vec![DynCompatibilityViolationKind::GAT])],
);
}

View file

@ -38,11 +38,11 @@ pub mod consteval;
pub mod db;
pub mod diagnostics;
pub mod display;
pub mod dyn_compatibility;
pub mod lang_items;
pub mod layout;
pub mod method_resolution;
pub mod mir;
pub mod object_safety;
pub mod primitive;
pub mod traits;

View file

@ -144,9 +144,9 @@ pub use {
hir_ty::{
consteval::ConstEvalError,
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
layout::LayoutError,
mir::{MirEvalError, MirLowerError},
object_safety::{MethodViolationCode, ObjectSafetyViolation},
CastError, FnAbi, PointerCast, Safety,
},
// FIXME: Properly encapsulate mir
@ -2690,8 +2690,8 @@ impl Trait {
.count()
}
pub fn object_safety(&self, db: &dyn HirDatabase) -> Option<ObjectSafetyViolation> {
hir_ty::object_safety::object_safety(db, self.id)
pub fn dyn_compatibility(&self, db: &dyn HirDatabase) -> Option<DynCompatibilityViolation> {
hir_ty::dyn_compatibility::dyn_compatibility(db, self.id)
}
fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {

View file

@ -7081,8 +7081,8 @@ This feature has no tracking issue, and is therefore likely internal to the comp
"##,
},
Lint {
label: "object_safe_for_dispatch",
description: r##"# `object_safe_for_dispatch`
label: "dyn_compatible_for_dispatch",
description: r##"# `dyn_compatible_for_dispatch`
The tracking issue for this feature is: [#43561]

View file

@ -3,9 +3,9 @@ use std::{mem, ops::Not};
use either::Either;
use hir::{
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind, HasCrate, HasSource,
HirDisplay, Layout, LayoutError, MethodViolationCode, Name, ObjectSafetyViolation, Semantics,
Trait, Type, TypeInfo,
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind,
DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError,
MethodViolationCode, Name, Semantics, Trait, Type, TypeInfo,
};
use ide_db::{
base_db::SourceDatabase,
@ -529,10 +529,10 @@ pub(super) fn definition(
_ => None,
};
let object_safety_info = if let Definition::Trait(it) = def {
let mut object_safety_info = String::new();
render_object_safety(db, &mut object_safety_info, it.object_safety(db));
Some(object_safety_info)
let dyn_compatibility_info = if let Definition::Trait(it) = def {
let mut dyn_compatibility_info = String::new();
render_dyn_compatibility(db, &mut dyn_compatibility_info, it.dyn_compatibility(db));
Some(dyn_compatibility_info)
} else {
None
};
@ -546,8 +546,8 @@ pub(super) fn definition(
desc.push_str(&layout_info);
desc.push('\n');
}
if let Some(object_safety_info) = object_safety_info {
desc.push_str(&object_safety_info);
if let Some(dyn_compatibility_info) = dyn_compatibility_info {
desc.push_str(&dyn_compatibility_info);
desc.push('\n');
}
desc.push_str(&label);
@ -980,24 +980,24 @@ fn keyword_hints(
}
}
fn render_object_safety(
fn render_dyn_compatibility(
db: &RootDatabase,
buf: &mut String,
safety: Option<ObjectSafetyViolation>,
safety: Option<DynCompatibilityViolation>,
) {
let Some(osv) = safety else {
buf.push_str("// Object Safety: Yes");
buf.push_str("// Dyn Compatible: Yes");
return;
};
buf.push_str("// Object Safety: No\n// - Reason: ");
buf.push_str("// Dyn Compatible: No\n// - Reason: ");
match osv {
ObjectSafetyViolation::SizedSelf => {
DynCompatibilityViolation::SizedSelf => {
buf.push_str("has a `Self: Sized` bound");
}
ObjectSafetyViolation::SelfReferential => {
DynCompatibilityViolation::SelfReferential => {
buf.push_str("has a bound that references `Self`");
}
ObjectSafetyViolation::Method(func, mvc) => {
DynCompatibilityViolation::Method(func, mvc) => {
let name = hir::Function::from(func).name(db);
format_to!(
buf,
@ -1020,7 +1020,7 @@ fn render_object_safety(
};
buf.push_str(desc);
}
ObjectSafetyViolation::AssocConst(const_) => {
DynCompatibilityViolation::AssocConst(const_) => {
let name = hir::Const::from(const_).name(db);
if let Some(name) = name {
format_to!(buf, "has an associated constant `{}`", name.as_str());
@ -1028,11 +1028,11 @@ fn render_object_safety(
buf.push_str("has an associated constant");
}
}
ObjectSafetyViolation::GAT(alias) => {
DynCompatibilityViolation::GAT(alias) => {
let name = hir::TypeAlias::from(alias).name(db);
format_to!(buf, "has a generic associated type `{}`", name.as_str());
}
ObjectSafetyViolation::HasNonSafeSuperTrait(super_trait) => {
DynCompatibilityViolation::HasNonCompatibleSuperTrait(super_trait) => {
let name = hir::Trait::from(super_trait).name(db);
format_to!(buf, "has a object unsafe supertrait `{}`", name.as_str());
}

View file

@ -7175,7 +7175,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: Yes
// Dyn Compatible: Yes
trait T {}
```
"#]],
@ -7195,7 +7195,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: Yes
// Dyn Compatible: Yes
trait T {}
```
"#]],
@ -7219,7 +7219,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: No
// Dyn Compatible: No
// - Reason: has a method `func` that is non dispatchable because of:
// - missing a receiver
trait T { /**/ }
@ -7245,7 +7245,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: No
// Dyn Compatible: No
// - Reason: has a method `func` that is non dispatchable because of:
// - missing a receiver
trait T {
@ -7275,7 +7275,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: No
// Dyn Compatible: No
// - Reason: has a method `func` that is non dispatchable because of:
// - missing a receiver
trait T {
@ -7305,7 +7305,7 @@ impl T$0 for () {}
```
```rust
// Object Safety: No
// Dyn Compatible: No
// - Reason: has a method `func` that is non dispatchable because of:
// - missing a receiver
trait T {

View file

@ -167,7 +167,7 @@ mod support {
}
#[test]
fn assert_ast_is_object_safe() {
fn assert_ast_is_dyn_compatible() {
fn _f(_: &dyn AstNode, _: &dyn HasName) {}
}

View file

@ -1,4 +1,4 @@
//! Object safe interface for file watching and reading.
//! Dynamically compatible interface for file watching and reading.
use std::fmt;
use paths::{AbsPath, AbsPathBuf};
@ -232,6 +232,6 @@ impl fmt::Debug for Message {
}
#[test]
fn handle_is_object_safe() {
fn handle_is_dyn_compatible() {
fn _assert(_: &dyn Handle) {}
}

View file

@ -378,7 +378,7 @@ impl AstNode for AssocItem {
}
```
Shared AST substructures are modeled via (object safe) traits:
Shared AST substructures are modeled via (dynamically compatible) traits:
```rust
trait HasVisibility: AstNode {