Auto merge of #15891 - Veykril:orphan-impls, r=Veykril

feat: Diagnose some orphan trait impl cases
This commit is contained in:
bors 2023-11-14 17:55:30 +00:00
commit 6e4538a6e9
23 changed files with 382 additions and 159 deletions

View file

@ -54,7 +54,7 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
let mut p = Printer { db, body, buf: header, indent_level: 0, needs_indent: false }; let mut p = Printer { db, body, buf: header, indent_level: 0, needs_indent: false };
if let DefWithBodyId::FunctionId(it) = owner { if let DefWithBodyId::FunctionId(it) = owner {
p.buf.push('('); p.buf.push('(');
body.params.iter().zip(&db.function_data(it).params).for_each(|(&param, ty)| { body.params.iter().zip(db.function_data(it).params.iter()).for_each(|(&param, ty)| {
p.print_pat(param); p.print_pat(param);
p.buf.push(':'); p.buf.push(':');
p.print_type_ref(ty); p.print_type_ref(ty);

View file

@ -34,7 +34,7 @@ use crate::{
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionData { pub struct FunctionData {
pub name: Name, pub name: Name,
pub params: Vec<Interned<TypeRef>>, pub params: Box<[Interned<TypeRef>]>,
pub ret_type: Interned<TypeRef>, pub ret_type: Interned<TypeRef>,
pub attrs: Attrs, pub attrs: Attrs,
pub visibility: RawVisibility, pub visibility: RawVisibility,
@ -177,7 +177,7 @@ pub struct TypeAliasData {
pub rustc_has_incoherent_inherent_impls: bool, pub rustc_has_incoherent_inherent_impls: bool,
pub rustc_allow_incoherent_impl: bool, pub rustc_allow_incoherent_impl: bool,
/// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl). /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
pub bounds: Vec<Interned<TypeBound>>, pub bounds: Box<[Interned<TypeBound>]>,
} }
impl TypeAliasData { impl TypeAliasData {
@ -210,7 +210,7 @@ impl TypeAliasData {
is_extern: matches!(loc.container, ItemContainerId::ExternBlockId(_)), is_extern: matches!(loc.container, ItemContainerId::ExternBlockId(_)),
rustc_has_incoherent_inherent_impls, rustc_has_incoherent_inherent_impls,
rustc_allow_incoherent_impl, rustc_allow_incoherent_impl,
bounds: typ.bounds.to_vec(), bounds: typ.bounds.clone(),
}) })
} }
} }

View file

@ -227,7 +227,7 @@ impl GenericParams {
let mut expander = Lazy::new(|| { let mut expander = Lazy::new(|| {
(module.def_map(db), Expander::new(db, loc.source(db).file_id, module)) (module.def_map(db), Expander::new(db, loc.source(db).file_id, module))
}); });
for param in &func_data.params { for param in func_data.params.iter() {
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param); generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
} }

View file

@ -396,14 +396,7 @@ impl<'a> Ctx<'a> {
let bounds = self.lower_type_bounds(type_alias); let bounds = self.lower_type_bounds(type_alias);
let generic_params = self.lower_generic_params(HasImplicitSelf::No, type_alias); let generic_params = self.lower_generic_params(HasImplicitSelf::No, type_alias);
let ast_id = self.source_ast_id_map.ast_id(type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias);
let res = TypeAlias { let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id };
name,
visibility,
bounds: bounds.into_boxed_slice(),
generic_params,
type_ref,
ast_id,
};
Some(id(self.data().type_aliases.alloc(res))) Some(id(self.data().type_aliases.alloc(res)))
} }
@ -637,13 +630,13 @@ impl<'a> Ctx<'a> {
Interned::new(generics) Interned::new(generics)
} }
fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>> { fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Box<[Interned<TypeBound>]> {
match node.type_bound_list() { match node.type_bound_list() {
Some(bound_list) => bound_list Some(bound_list) => bound_list
.bounds() .bounds()
.map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it))) .map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
.collect(), .collect(),
None => Vec::new(), None => Box::default(),
} }
} }

View file

@ -20,8 +20,8 @@ use crate::{
method_resolution::{InherentImpls, TraitImpls, TyFingerprint}, method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
mir::{BorrowckResult, MirBody, MirLowerError}, mir::{BorrowckResult, MirBody, MirLowerError},
Binders, CallableDefId, ClosureId, Const, FnDefId, GenericArg, ImplTraitId, InferenceResult, Binders, CallableDefId, ClosureId, Const, FnDefId, GenericArg, ImplTraitId, InferenceResult,
Interner, PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, Substitution, TraitRef, Ty, Interner, PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, Substitution,
TyDefId, ValueTyDefId, TraitEnvironment, TraitRef, Ty, TyDefId, ValueTyDefId,
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
@ -47,7 +47,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
&self, &self,
def: DefWithBodyId, def: DefWithBodyId,
subst: Substitution, subst: Substitution,
env: Arc<crate::TraitEnvironment>, env: Arc<TraitEnvironment>,
) -> Result<Arc<MirBody>, MirLowerError>; ) -> Result<Arc<MirBody>, MirLowerError>;
#[salsa::invoke(crate::mir::monomorphized_mir_body_for_closure_query)] #[salsa::invoke(crate::mir::monomorphized_mir_body_for_closure_query)]
@ -55,7 +55,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
&self, &self,
def: ClosureId, def: ClosureId,
subst: Substitution, subst: Substitution,
env: Arc<crate::TraitEnvironment>, env: Arc<TraitEnvironment>,
) -> Result<Arc<MirBody>, MirLowerError>; ) -> Result<Arc<MirBody>, MirLowerError>;
#[salsa::invoke(crate::mir::borrowck_query)] #[salsa::invoke(crate::mir::borrowck_query)]
@ -81,7 +81,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
&self, &self,
def: GeneralConstId, def: GeneralConstId,
subst: Substitution, subst: Substitution,
trait_env: Option<Arc<crate::TraitEnvironment>>, trait_env: Option<Arc<TraitEnvironment>>,
) -> Result<Const, ConstEvalError>; ) -> Result<Const, ConstEvalError>;
#[salsa::invoke(crate::consteval::const_eval_static_query)] #[salsa::invoke(crate::consteval::const_eval_static_query)]
@ -104,16 +104,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
&self, &self,
def: AdtId, def: AdtId,
subst: Substitution, subst: Substitution,
env: Arc<crate::TraitEnvironment>, env: Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError>; ) -> Result<Arc<Layout>, LayoutError>;
#[salsa::invoke(crate::layout::layout_of_ty_query)] #[salsa::invoke(crate::layout::layout_of_ty_query)]
#[salsa::cycle(crate::layout::layout_of_ty_recover)] #[salsa::cycle(crate::layout::layout_of_ty_recover)]
fn layout_of_ty( fn layout_of_ty(&self, ty: Ty, env: Arc<TraitEnvironment>) -> Result<Arc<Layout>, LayoutError>;
&self,
ty: Ty,
env: Arc<crate::TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError>;
#[salsa::invoke(crate::layout::target_data_layout_query)] #[salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>; fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
@ -121,7 +117,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(crate::method_resolution::lookup_impl_method_query)] #[salsa::invoke(crate::method_resolution::lookup_impl_method_query)]
fn lookup_impl_method( fn lookup_impl_method(
&self, &self,
env: Arc<crate::TraitEnvironment>, env: Arc<TraitEnvironment>,
func: FunctionId, func: FunctionId,
fn_subst: Substitution, fn_subst: Substitution,
) -> (FunctionId, Substitution); ) -> (FunctionId, Substitution);
@ -149,10 +145,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(crate::lower::trait_environment_for_body_query)] #[salsa::invoke(crate::lower::trait_environment_for_body_query)]
#[salsa::transparent] #[salsa::transparent]
fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<crate::TraitEnvironment>; fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<TraitEnvironment>;
#[salsa::invoke(crate::lower::trait_environment_query)] #[salsa::invoke(crate::lower::trait_environment_query)]
fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>; fn trait_environment(&self, def: GenericDefId) -> Arc<TraitEnvironment>;
#[salsa::invoke(crate::lower::generic_defaults_query)] #[salsa::invoke(crate::lower::generic_defaults_query)]
#[salsa::cycle(crate::lower::generic_defaults_recover)] #[salsa::cycle(crate::lower::generic_defaults_recover)]
@ -249,7 +245,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
fn normalize_projection( fn normalize_projection(
&self, &self,
projection: crate::ProjectionTy, projection: crate::ProjectionTy,
env: Arc<crate::TraitEnvironment>, env: Arc<TraitEnvironment>,
) -> Ty; ) -> Ty;
#[salsa::invoke(trait_solve_wait)] #[salsa::invoke(trait_solve_wait)]

View file

@ -11,9 +11,3 @@ pub use crate::diagnostics::{
}, },
unsafe_check::{missing_unsafe, unsafe_expressions, UnsafeExpr}, unsafe_check::{missing_unsafe, unsafe_expressions, UnsafeExpr},
}; };
#[derive(Debug, PartialEq, Eq)]
pub struct IncoherentImpl {
pub file_id: hir_expand::HirFileId,
pub impl_: syntax::AstPtr<syntax::ast::Impl>,
}

View file

@ -1,5 +1,7 @@
//! Compute the binary representation of a type //! Compute the binary representation of a type
use std::fmt;
use chalk_ir::{AdtId, FloatTy, IntTy, TyKind, UintTy}; use chalk_ir::{AdtId, FloatTy, IntTy, TyKind, UintTy};
use hir_def::{ use hir_def::{
layout::{ layout::{
@ -26,12 +28,6 @@ pub use self::{
target::target_data_layout_query, target::target_data_layout_query,
}; };
macro_rules! user_error {
($it: expr) => {
return Err(LayoutError::UserError(format!($it).into()))
};
}
mod adt; mod adt;
mod target; mod target;
@ -73,13 +69,38 @@ pub type Variants = hir_def::layout::Variants<RustcFieldIdx, RustcEnumVariantIdx
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum LayoutError { pub enum LayoutError {
UserError(Box<str>), HasErrorConst,
HasErrorType,
HasPlaceholder,
InvalidSimdType,
NotImplemented,
RecursiveTypeWithoutIndirection,
SizeOverflow, SizeOverflow,
TargetLayoutNotAvailable, TargetLayoutNotAvailable,
HasPlaceholder,
HasErrorType,
NotImplemented,
Unknown, Unknown,
UserReprTooSmall,
}
impl std::error::Error for LayoutError {}
impl fmt::Display for LayoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LayoutError::HasErrorConst => write!(f, "type contains an unevaluatable const"),
LayoutError::HasErrorType => write!(f, "type contains an error"),
LayoutError::HasPlaceholder => write!(f, "type contains placeholders"),
LayoutError::InvalidSimdType => write!(f, "invalid simd type definition"),
LayoutError::NotImplemented => write!(f, "not implemented"),
LayoutError::RecursiveTypeWithoutIndirection => {
write!(f, "recursive type without indirection")
}
LayoutError::SizeOverflow => write!(f, "size overflow"),
LayoutError::TargetLayoutNotAvailable => write!(f, "target layout not available"),
LayoutError::Unknown => write!(f, "unknown"),
LayoutError::UserReprTooSmall => {
write!(f, "the `#[repr]` hint is too small to hold the discriminants of the enum")
}
}
}
} }
struct LayoutCx<'a> { struct LayoutCx<'a> {
@ -118,9 +139,7 @@ fn layout_of_simd_ty(
let f0_ty = match fields.iter().next() { let f0_ty = match fields.iter().next() {
Some(it) => it.1.clone().substitute(Interner, subst), Some(it) => it.1.clone().substitute(Interner, subst),
None => { None => return Err(LayoutError::InvalidSimdType),
user_error!("simd type with zero fields");
}
}; };
// The element type and number of elements of the SIMD vector // The element type and number of elements of the SIMD vector
@ -134,7 +153,7 @@ fn layout_of_simd_ty(
// Extract the number of elements from the layout of the array field: // Extract the number of elements from the layout of the array field:
let FieldsShape::Array { count, .. } = db.layout_of_ty(f0_ty.clone(), env.clone())?.fields let FieldsShape::Array { count, .. } = db.layout_of_ty(f0_ty.clone(), env.clone())?.fields
else { else {
user_error!("Array with non array layout"); return Err(LayoutError::Unknown);
}; };
(e_ty.clone(), count, true) (e_ty.clone(), count, true)
@ -146,7 +165,7 @@ fn layout_of_simd_ty(
// Compute the ABI of the element type: // Compute the ABI of the element type:
let e_ly = db.layout_of_ty(e_ty, env.clone())?; let e_ly = db.layout_of_ty(e_ty, env.clone())?;
let Abi::Scalar(e_abi) = e_ly.abi else { let Abi::Scalar(e_abi) = e_ly.abi else {
user_error!("simd type with inner non scalar type"); return Err(LayoutError::Unknown);
}; };
// Compute the size and alignment of the vector: // Compute the size and alignment of the vector:
@ -259,9 +278,7 @@ pub fn layout_of_ty_query(
cx.univariant(dl, &fields, &ReprOptions::default(), kind).ok_or(LayoutError::Unknown)? cx.univariant(dl, &fields, &ReprOptions::default(), kind).ok_or(LayoutError::Unknown)?
} }
TyKind::Array(element, count) => { TyKind::Array(element, count) => {
let count = try_const_usize(db, &count).ok_or(LayoutError::UserError(Box::from( let count = try_const_usize(db, &count).ok_or(LayoutError::HasErrorConst)? as u64;
"unevaluated or mistyped const generic parameter",
)))? as u64;
let element = db.layout_of_ty(element.clone(), trait_env.clone())?; let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?; let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;
@ -352,7 +369,7 @@ pub fn layout_of_ty_query(
let mut unit = layout_of_unit(&cx, dl)?; let mut unit = layout_of_unit(&cx, dl)?;
match unit.abi { match unit.abi {
Abi::Aggregate { ref mut sized } => *sized = false, Abi::Aggregate { ref mut sized } => *sized = false,
_ => user_error!("bug"), _ => return Err(LayoutError::Unknown),
} }
unit unit
} }
@ -418,7 +435,7 @@ pub fn layout_of_ty_recover(
_: &Ty, _: &Ty,
_: &Arc<TraitEnvironment>, _: &Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> { ) -> Result<Arc<Layout>, LayoutError> {
user_error!("infinite sized recursive type"); Err(LayoutError::RecursiveTypeWithoutIndirection)
} }
fn layout_of_unit(cx: &LayoutCx<'_>, dl: &TargetDataLayout) -> Result<Layout, LayoutError> { fn layout_of_unit(cx: &LayoutCx<'_>, dl: &TargetDataLayout) -> Result<Layout, LayoutError> {

View file

@ -145,7 +145,7 @@ pub fn layout_of_adt_recover(
_: &Substitution, _: &Substitution,
_: &Arc<TraitEnvironment>, _: &Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> { ) -> Result<Arc<Layout>, LayoutError> {
user_error!("infinite sized recursive type"); Err(LayoutError::RecursiveTypeWithoutIndirection)
} }
/// Finds the appropriate Integer type and signedness for the given /// Finds the appropriate Integer type and signedness for the given
@ -169,11 +169,7 @@ fn repr_discr(
let discr = Integer::from_attr(dl, ity); let discr = Integer::from_attr(dl, ity);
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit }; let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
if discr < fit { if discr < fit {
return Err(LayoutError::UserError( return Err(LayoutError::UserReprTooSmall);
"Integer::repr_discr: `#[repr]` hint too small for \
discriminant range of enum "
.into(),
));
} }
return Ok((discr, ity.is_signed())); return Ok((discr, ity.is_signed()));
} }

View file

@ -210,16 +210,13 @@ fn recursive() {
struct BoxLike<T: ?Sized>(*mut T); struct BoxLike<T: ?Sized>(*mut T);
struct Goal(BoxLike<Goal>); struct Goal(BoxLike<Goal>);
} }
check_fail( check_fail(r#"struct Goal(Goal);"#, LayoutError::RecursiveTypeWithoutIndirection);
r#"struct Goal(Goal);"#,
LayoutError::UserError("infinite sized recursive type".into()),
);
check_fail( check_fail(
r#" r#"
struct Foo<T>(Foo<T>); struct Foo<T>(Foo<T>);
struct Goal(Foo<i32>); struct Goal(Foo<i32>);
"#, "#,
LayoutError::UserError("infinite sized recursive type".into()), LayoutError::RecursiveTypeWithoutIndirection,
); );
} }

View file

@ -80,6 +80,7 @@ pub use mapping::{
lt_from_placeholder_idx, to_assoc_type_id, to_chalk_trait_id, to_foreign_def_id, lt_from_placeholder_idx, to_assoc_type_id, to_chalk_trait_id, to_foreign_def_id,
to_placeholder_idx, to_placeholder_idx,
}; };
pub use method_resolution::check_orphan_rules;
pub use traits::TraitEnvironment; pub use traits::TraitEnvironment;
pub use utils::{all_super_traits, is_fn_unsafe_to_call}; pub use utils::{all_super_traits, is_fn_unsafe_to_call};

View file

@ -1383,51 +1383,50 @@ pub(crate) fn generic_predicates_for_param_query(
let ctx = TyLoweringContext::new(db, &resolver, def.into()) let ctx = TyLoweringContext::new(db, &resolver, def.into())
.with_type_param_mode(ParamLoweringMode::Variable); .with_type_param_mode(ParamLoweringMode::Variable);
let generics = generics(db.upcast(), def); let generics = generics(db.upcast(), def);
// we have to filter out all other predicates *first*, before attempting to lower them
let predicate = |pred: &&_| match pred {
WherePredicate::ForLifetime { target, bound, .. }
| WherePredicate::TypeBound { target, bound, .. } => {
let invalid_target = match target {
WherePredicateTypeTarget::TypeRef(type_ref) => {
ctx.lower_ty_only_param(type_ref) != Some(param_id)
}
&WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
let target_id = TypeOrConstParamId { parent: def, local_id };
target_id != param_id
}
};
if invalid_target {
return false;
}
match &**bound {
TypeBound::ForLifetime(_, path) | TypeBound::Path(path, _) => {
// Only lower the bound if the trait could possibly define the associated
// type we're looking for.
let Some(assoc_name) = &assoc_name else { return true };
let Some(TypeNs::TraitId(tr)) =
resolver.resolve_path_in_type_ns_fully(db.upcast(), path)
else {
return false;
};
all_super_traits(db.upcast(), tr).iter().any(|tr| {
db.trait_data(*tr).items.iter().any(|(name, item)| {
matches!(item, AssocItemId::TypeAliasId(_)) && name == assoc_name
})
})
}
TypeBound::Lifetime(_) | TypeBound::Error => false,
}
}
WherePredicate::Lifetime { .. } => false,
};
let mut predicates: Vec<_> = resolver let mut predicates: Vec<_> = resolver
.where_predicates_in_scope() .where_predicates_in_scope()
// we have to filter out all other predicates *first*, before attempting to lower them .filter(predicate)
.filter(|pred| match pred {
WherePredicate::ForLifetime { target, bound, .. }
| WherePredicate::TypeBound { target, bound, .. } => {
match target {
WherePredicateTypeTarget::TypeRef(type_ref) => {
if ctx.lower_ty_only_param(type_ref) != Some(param_id) {
return false;
}
}
&WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
let target_id = TypeOrConstParamId { parent: def, local_id };
if target_id != param_id {
return false;
}
}
};
match &**bound {
TypeBound::ForLifetime(_, path) | TypeBound::Path(path, _) => {
// Only lower the bound if the trait could possibly define the associated
// type we're looking for.
let assoc_name = match &assoc_name {
Some(it) => it,
None => return true,
};
let tr = match resolver.resolve_path_in_type_ns_fully(db.upcast(), path) {
Some(TypeNs::TraitId(tr)) => tr,
_ => return false,
};
all_super_traits(db.upcast(), tr).iter().any(|tr| {
db.trait_data(*tr).items.iter().any(|(name, item)| {
matches!(item, AssocItemId::TypeAliasId(_)) && name == assoc_name
})
})
}
TypeBound::Lifetime(_) | TypeBound::Error => false,
}
}
WherePredicate::Lifetime { .. } => false,
})
.flat_map(|pred| { .flat_map(|pred| {
ctx.lower_where_predicate(pred, true).map(|p| make_binders(db, &generics, p)) ctx.lower_where_predicate(pred, true).map(|p| make_binders(db, &generics, p))
}) })
@ -1519,7 +1518,12 @@ pub(crate) fn trait_environment_query(
let env = chalk_ir::Environment::new(Interner).add_clauses(Interner, clauses); let env = chalk_ir::Environment::new(Interner).add_clauses(Interner, clauses);
Arc::new(TraitEnvironment { krate, block: None, traits_from_clauses: traits_in_scope, env }) Arc::new(TraitEnvironment {
krate,
block: None,
traits_from_clauses: traits_in_scope.into_boxed_slice(),
env,
})
} }
/// Resolve the where clause(s) of an item with generics. /// Resolve the where clause(s) of an item with generics.

View file

@ -862,6 +862,62 @@ fn is_inherent_impl_coherent(
} }
} }
/// Checks whether the impl satisfies the orphan rules.
///
/// Given `impl<P1..=Pn> Trait<T1..=Tn> for T0`, an `impl`` is valid only if at least one of the following is true:
/// - Trait is a local trait
/// - All of
/// - At least one of the types `T0..=Tn`` must be a local type. Let `Ti`` be the first such type.
/// - No uncovered type parameters `P1..=Pn` may appear in `T0..Ti`` (excluding `Ti`)
pub fn check_orphan_rules(db: &dyn HirDatabase, impl_: ImplId) -> bool {
let substs = TyBuilder::placeholder_subst(db, impl_);
let Some(impl_trait) = db.impl_trait(impl_) else {
// not a trait impl
return true;
};
let local_crate = impl_.lookup(db.upcast()).container.krate();
let is_local = |tgt_crate| tgt_crate == local_crate;
let trait_ref = impl_trait.substitute(Interner, &substs);
let trait_id = from_chalk_trait_id(trait_ref.trait_id);
if is_local(trait_id.module(db.upcast()).krate()) {
// trait to be implemented is local
return true;
}
let unwrap_fundamental = |ty: Ty| match ty.kind(Interner) {
TyKind::Ref(_, _, referenced) => referenced.clone(),
&TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), ref subs) => {
let struct_data = db.struct_data(s);
if struct_data.flags.contains(StructFlags::IS_FUNDAMENTAL) {
let next = subs.type_parameters(Interner).next();
match next {
Some(ty) => ty,
None => ty,
}
} else {
ty
}
}
_ => ty,
};
// - At least one of the types `T0..=Tn`` must be a local type. Let `Ti`` be the first such type.
let is_not_orphan = trait_ref.substitution.type_parameters(Interner).any(|ty| {
match unwrap_fundamental(ty).kind(Interner) {
&TyKind::Adt(AdtId(id), _) => is_local(id.module(db.upcast()).krate()),
TyKind::Error => true,
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
is_local(from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate())
}),
_ => false,
}
});
// FIXME: param coverage
// - No uncovered type parameters `P1..=Pn` may appear in `T0..Ti`` (excluding `Ti`)
is_not_orphan
}
pub fn iterate_path_candidates( pub fn iterate_path_candidates(
ty: &Canonical<Ty>, ty: &Canonical<Ty>,
db: &dyn HirDatabase, db: &dyn HirDatabase,

View file

@ -4439,42 +4439,42 @@ fn test(v: S<i32>) {
fn associated_type_in_argument() { fn associated_type_in_argument() {
check( check(
r#" r#"
trait A { trait A {
fn m(&self) -> i32; fn m(&self) -> i32;
} }
fn x<T: B>(k: &<T as B>::Ty) { fn x<T: B>(k: &<T as B>::Ty) {
k.m(); k.m();
} }
struct X; struct X;
struct Y; struct Y;
impl A for X { impl A for X {
fn m(&self) -> i32 { fn m(&self) -> i32 {
8 8
}
} }
}
impl A for Y { impl A for Y {
fn m(&self) -> i32 { fn m(&self) -> i32 {
32 32
}
} }
}
trait B { trait B {
type Ty: A; type Ty: A;
} }
impl B for u16 { impl B for u16 {
type Ty = X; type Ty = X;
} }
fn ttt() { fn ttt() {
let inp = Y; let inp = Y;
x::<u16>(&inp); x::<u16>(&inp);
//^^^^ expected &X, got &Y //^^^^ expected &X, got &Y
} }
"#, "#,
); );
} }

View file

@ -48,7 +48,7 @@ pub struct TraitEnvironment {
pub krate: CrateId, pub krate: CrateId,
pub block: Option<BlockId>, pub block: Option<BlockId>,
// FIXME make this a BTreeMap // FIXME make this a BTreeMap
pub(crate) traits_from_clauses: Vec<(Ty, TraitId)>, pub(crate) traits_from_clauses: Box<[(Ty, TraitId)]>,
pub env: chalk_ir::Environment<Interner>, pub env: chalk_ir::Environment<Interner>,
} }
@ -57,7 +57,7 @@ impl TraitEnvironment {
TraitEnvironment { TraitEnvironment {
krate, krate,
block: None, block: None,
traits_from_clauses: Vec::new(), traits_from_clauses: Box::default(),
env: chalk_ir::Environment::new(Interner), env: chalk_ir::Environment::new(Interner),
} }
} }

View file

@ -3,7 +3,7 @@
//! //!
//! This probably isn't the best way to do this -- ideally, diagnostics should //! This probably isn't the best way to do this -- ideally, diagnostics should
//! be expressed in terms of hir types themselves. //! be expressed in terms of hir types themselves.
pub use hir_ty::diagnostics::{CaseType, IncoherentImpl, IncorrectCase}; pub use hir_ty::diagnostics::{CaseType, IncorrectCase};
use base_db::CrateId; use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions}; use cfg::{CfgExpr, CfgOptions};
@ -38,6 +38,7 @@ diagnostics![
IncorrectCase, IncorrectCase,
InvalidDeriveTarget, InvalidDeriveTarget,
IncoherentImpl, IncoherentImpl,
TraitImplOrphan,
MacroDefError, MacroDefError,
MacroError, MacroError,
MacroExpansionParseError, MacroExpansionParseError,
@ -280,3 +281,15 @@ pub struct MovedOutOfRef {
pub ty: Type, pub ty: Type,
pub span: InFile<SyntaxNodePtr>, pub span: InFile<SyntaxNodePtr>,
} }
#[derive(Debug, PartialEq, Eq)]
pub struct IncoherentImpl {
pub file_id: HirFileId,
pub impl_: AstPtr<ast::Impl>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct TraitImplOrphan {
pub file_id: HirFileId,
pub impl_: AstPtr<ast::Impl>,
}

View file

@ -616,7 +616,7 @@ impl HirDisplay for TypeAlias {
write_where_clause(def_id, f)?; write_where_clause(def_id, f)?;
if !data.bounds.is_empty() { if !data.bounds.is_empty() {
f.write_str(": ")?; f.write_str(": ")?;
f.write_joined(&data.bounds, " + ")?; f.write_joined(data.bounds.iter(), " + ")?;
} }
if let Some(ty) = &data.type_ref { if let Some(ty) = &data.type_ref {
f.write_str(" = ")?; f.write_str(" = ")?;

View file

@ -60,7 +60,7 @@ use hir_def::{
}; };
use hir_expand::{name::name, MacroCallKind}; use hir_expand::{name::name, MacroCallKind};
use hir_ty::{ use hir_ty::{
all_super_traits, autoderef, all_super_traits, autoderef, check_orphan_rules,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt}, consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic, diagnostics::BodyValidationDiagnostic,
known_const_to_ast, known_const_to_ast,
@ -95,7 +95,7 @@ pub use crate::{
MacroExpansionParseError, MalformedDerive, MismatchedArgCount, MacroExpansionParseError, MalformedDerive, MismatchedArgCount,
MismatchedTupleStructPatArgCount, MissingFields, MissingMatchArms, MissingUnsafe, MismatchedTupleStructPatArgCount, MissingFields, MissingMatchArms, MissingUnsafe,
MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem, PrivateField, MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem, PrivateField,
ReplaceFilterMapNextWithFindMap, TypeMismatch, TypedHole, UndeclaredLabel, ReplaceFilterMapNextWithFindMap, TraitImplOrphan, TypeMismatch, TypedHole, UndeclaredLabel,
UnimplementedBuiltinMacro, UnreachableLabel, UnresolvedExternCrate, UnresolvedField, UnimplementedBuiltinMacro, UnreachableLabel, UnresolvedExternCrate, UnresolvedField,
UnresolvedImport, UnresolvedMacroCall, UnresolvedMethodCall, UnresolvedModule, UnresolvedImport, UnresolvedMacroCall, UnresolvedMethodCall, UnresolvedModule,
UnresolvedProcMacro, UnusedMut, UnusedVariable, UnresolvedProcMacro, UnusedMut, UnusedVariable,
@ -624,6 +624,11 @@ impl Module {
acc.push(IncoherentImpl { impl_: ast_id_map.get(node.ast_id()), file_id }.into()) acc.push(IncoherentImpl { impl_: ast_id_map.get(node.ast_id()), file_id }.into())
} }
if !impl_def.check_orphan_rules(db) {
let ast_id_map = db.ast_id_map(file_id);
acc.push(TraitImplOrphan { impl_: ast_id_map.get(node.ast_id()), file_id }.into())
}
for item in impl_def.items(db) { for item in impl_def.items(db) {
let def: DefWithBody = match item { let def: DefWithBody = match item {
AssocItem::Function(it) => it.into(), AssocItem::Function(it) => it.into(),
@ -3398,6 +3403,10 @@ impl Impl {
db.impl_data(self.id).is_negative db.impl_data(self.id).is_negative
} }
pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
db.impl_data(self.id).is_unique()
}
pub fn module(self, db: &dyn HirDatabase) -> Module { pub fn module(self, db: &dyn HirDatabase) -> Module {
self.id.lookup(db.upcast()).container.into() self.id.lookup(db.upcast()).container.into()
} }
@ -3406,6 +3415,10 @@ impl Impl {
let src = self.source(db)?; let src = self.source(db)?;
src.file_id.as_builtin_derive_attr_node(db.upcast()) src.file_id.as_builtin_derive_attr_node(db.upcast())
} }
pub fn check_orphan_rules(self, db: &dyn HirDatabase) -> bool {
check_orphan_rules(db, self.id)
}
} }
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]

View file

@ -0,0 +1,106 @@
use hir::InFile;
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
// Diagnostic: trait-impl-orphan
//
// Only traits defined in the current crate can be implemented for arbitrary types
pub(crate) fn trait_impl_orphan(
ctx: &DiagnosticsContext<'_>,
d: &hir::TraitImplOrphan,
) -> Diagnostic {
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::RustcHardError("E0117"),
format!("only traits defined in the current crate can be implemented for arbitrary types"),
InFile::new(d.file_id, d.impl_.clone().into()),
)
// Not yet checked for false positives
.experimental()
}
#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;
#[test]
fn simple() {
check_diagnostics(
r#"
//- /foo.rs crate:foo
pub trait Foo {}
//- /bar.rs crate:bar
pub struct Bar;
//- /main.rs crate:main deps:foo,bar
struct LocalType;
trait LocalTrait {}
impl foo::Foo for bar::Bar {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
impl foo::Foo for LocalType {}
impl LocalTrait for bar::Bar {}
"#,
);
}
#[test]
fn generics() {
check_diagnostics(
r#"
//- /foo.rs crate:foo
pub trait Foo<T> {}
//- /bar.rs crate:bar
pub struct Bar<T>(T);
//- /main.rs crate:main deps:foo,bar
struct LocalType<T>;
trait LocalTrait<T> {}
impl<T> foo::Foo<T> for bar::Bar<T> {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
impl<T> foo::Foo<T> for bar::Bar<LocalType<T>> {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
impl<T> foo::Foo<LocalType<T>> for bar::Bar<T> {}
impl<T> foo::Foo<bar::Bar<LocalType<T>>> for bar::Bar<LocalType<T>> {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
"#,
);
}
#[test]
fn fundamental() {
check_diagnostics(
r#"
//- /foo.rs crate:foo
pub trait Foo<T> {}
//- /bar.rs crate:bar
pub struct Bar<T>(T);
#[lang = "owned_box"]
#[fundamental]
pub struct Box<T>(T);
//- /main.rs crate:main deps:foo,bar
struct LocalType;
impl<T> foo::Foo<T> for bar::Box<T> {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
impl<T> foo::Foo<T> for &LocalType {}
impl<T> foo::Foo<T> for bar::Box<LocalType> {}
"#,
);
}
#[test]
fn dyn_object() {
check_diagnostics(
r#"
//- /foo.rs crate:foo
pub trait Foo<T> {}
//- /bar.rs crate:bar
pub struct Bar;
//- /main.rs crate:main deps:foo,bar
trait LocalTrait {}
impl<T> foo::Foo<T> for dyn LocalTrait {}
impl<T> foo::Foo<dyn LocalTrait> for Bar {}
"#,
);
}
}

View file

@ -44,6 +44,7 @@ mod handlers {
pub(crate) mod private_assoc_item; pub(crate) mod private_assoc_item;
pub(crate) mod private_field; pub(crate) mod private_field;
pub(crate) mod replace_filter_map_next_with_find_map; pub(crate) mod replace_filter_map_next_with_find_map;
pub(crate) mod trait_impl_orphan;
pub(crate) mod typed_hole; pub(crate) mod typed_hole;
pub(crate) mod type_mismatch; pub(crate) mod type_mismatch;
pub(crate) mod unimplemented_builtin_macro; pub(crate) mod unimplemented_builtin_macro;
@ -358,6 +359,7 @@ pub fn diagnostics(
AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d), AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d),
AnyDiagnostic::PrivateField(d) => handlers::private_field::private_field(&ctx, &d), AnyDiagnostic::PrivateField(d) => handlers::private_field::private_field(&ctx, &d),
AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d), AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
AnyDiagnostic::TraitImplOrphan(d) => handlers::trait_impl_orphan::trait_impl_orphan(&ctx, &d),
AnyDiagnostic::TypedHole(d) => handlers::typed_hole::typed_hole(&ctx, &d), AnyDiagnostic::TypedHole(d) => handlers::typed_hole::typed_hole(&ctx, &d),
AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d), AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d),
AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d), AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d),

View file

@ -5,7 +5,7 @@ use expect_test::Expect;
use ide_db::{ use ide_db::{
assists::AssistResolveStrategy, assists::AssistResolveStrategy,
base_db::{fixture::WithFixture, SourceDatabaseExt}, base_db::{fixture::WithFixture, SourceDatabaseExt},
RootDatabase, LineIndexDatabase, RootDatabase,
}; };
use stdx::trim_indent; use stdx::trim_indent;
use test_utils::{assert_eq_text, extract_annotations, MiniCore}; use test_utils::{assert_eq_text, extract_annotations, MiniCore};
@ -103,6 +103,7 @@ pub(crate) fn check_diagnostics(ra_fixture: &str) {
pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) { pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
let (db, files) = RootDatabase::with_many_files(ra_fixture); let (db, files) = RootDatabase::with_many_files(ra_fixture);
for file_id in files { for file_id in files {
let line_index = db.line_index(file_id);
let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id); let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
let expected = extract_annotations(&db.file_text(file_id)); let expected = extract_annotations(&db.file_text(file_id));
@ -136,8 +137,16 @@ pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixtur
} }
} }
if expected != actual { if expected != actual {
let fneg = expected.iter().filter(|x| !actual.contains(x)).collect::<Vec<_>>(); let fneg = expected
let fpos = actual.iter().filter(|x| !expected.contains(x)).collect::<Vec<_>>(); .iter()
.filter(|x| !actual.contains(x))
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
.collect::<Vec<_>>();
let fpos = actual
.iter()
.filter(|x| !expected.contains(x))
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
.collect::<Vec<_>>();
panic!("Diagnostic test failed.\nFalse negatives: {fneg:?}\nFalse positives: {fpos:?}"); panic!("Diagnostic test failed.\nFalse negatives: {fneg:?}\nFalse positives: {fpos:?}");
} }

View file

@ -563,6 +563,7 @@ mod tests {
use hir::ClosureStyle; use hir::ClosureStyle;
use itertools::Itertools; use itertools::Itertools;
use test_utils::extract_annotations; use test_utils::extract_annotations;
use text_edit::{TextRange, TextSize};
use crate::inlay_hints::{AdjustmentHints, AdjustmentHintsMode}; use crate::inlay_hints::{AdjustmentHints, AdjustmentHintsMode};
use crate::DiscriminantHints; use crate::DiscriminantHints;
@ -629,6 +630,22 @@ mod tests {
expect.assert_debug_eq(&inlay_hints) expect.assert_debug_eq(&inlay_hints)
} }
#[track_caller]
pub(super) fn check_expect_clear_loc(
config: InlayHintsConfig,
ra_fixture: &str,
expect: Expect,
) {
let (analysis, file_id) = fixture::file(ra_fixture);
let mut inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();
inlay_hints.iter_mut().flat_map(|hint| &mut hint.label.parts).for_each(|hint| {
if let Some(loc) = &mut hint.linked_location {
loc.range = TextRange::empty(TextSize::from(0));
}
});
expect.assert_debug_eq(&inlay_hints)
}
/// Computes inlay hints for the fixture, applies all the provided text edits and then runs /// Computes inlay hints for the fixture, applies all the provided text edits and then runs
/// expect test. /// expect test.
#[track_caller] #[track_caller]

View file

@ -78,7 +78,9 @@ mod tests {
use expect_test::expect; use expect_test::expect;
use crate::{ use crate::{
inlay_hints::tests::{check_expect, check_with_config, DISABLED_CONFIG, TEST_CONFIG}, inlay_hints::tests::{
check_expect, check_expect_clear_loc, check_with_config, DISABLED_CONFIG, TEST_CONFIG,
},
InlayHintsConfig, InlayHintsConfig,
}; };
@ -444,7 +446,7 @@ fn main() {
#[test] #[test]
fn shorten_iterator_chaining_hints() { fn shorten_iterator_chaining_hints() {
check_expect( check_expect_clear_loc(
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG }, InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
r#" r#"
//- minicore: iterators //- minicore: iterators
@ -484,7 +486,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10752..10760, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -497,7 +499,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10784..10788, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -522,7 +524,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10752..10760, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -535,7 +537,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10784..10788, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -560,7 +562,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10752..10760, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -573,7 +575,7 @@ fn main() {
file_id: FileId( file_id: FileId(
1, 1,
), ),
range: 10784..10788, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",
@ -598,7 +600,7 @@ fn main() {
file_id: FileId( file_id: FileId(
0, 0,
), ),
range: 24..30, range: 0..0,
}, },
), ),
tooltip: "", tooltip: "",

View file

@ -44,7 +44,7 @@
//! panic: fmt //! panic: fmt
//! phantom_data: //! phantom_data:
//! pin: //! pin:
//! pointee: //! pointee: copy, send, sync, ord, hash, unpin
//! range: //! range:
//! result: //! result:
//! send: sized //! send: sized
@ -54,6 +54,7 @@
//! sync: sized //! sync: sized
//! transmute: //! transmute:
//! try: infallible //! try: infallible
//! unpin: sized
//! unsize: sized //! unsize: sized
#![rustc_coherence_is_core] #![rustc_coherence_is_core]
@ -89,6 +90,11 @@ pub mod marker {
pub trait Unsize<T: ?Sized> {} pub trait Unsize<T: ?Sized> {}
// endregion:unsize // endregion:unsize
// region:unpin
#[lang = "unpin"]
pub auto trait Unpin {}
// endregion:unpin
// region:copy // region:copy
#[lang = "copy"] #[lang = "copy"]
pub trait Copy: Clone {} pub trait Copy: Clone {}
@ -387,9 +393,10 @@ pub mod ptr {
// region:pointee // region:pointee
#[lang = "pointee_trait"] #[lang = "pointee_trait"]
#[rustc_deny_explicit_impl(implement_via_object = false)]
pub trait Pointee { pub trait Pointee {
#[lang = "metadata_type"] #[lang = "metadata_type"]
type Metadata; type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
} }
// endregion:pointee // endregion:pointee
// region:non_null // region:non_null