|
1 | 1 | //! Orphan checker: every impl either implements a trait defined in this
|
2 | 2 | //! crate or pertains to a type defined in this crate.
|
3 | 3 |
|
| 4 | +use rustc_data_structures::fx::FxHashSet; |
4 | 5 | use rustc_errors::struct_span_err;
|
5 | 6 | use rustc_errors::ErrorReported;
|
6 | 7 | use rustc_hir as hir;
|
| 8 | +use rustc_index::bit_set::GrowableBitSet; |
7 | 9 | use rustc_infer::infer::TyCtxtInferExt;
|
8 |
| -use rustc_middle::ty::{self, TyCtxt}; |
9 |
| -use rustc_span::def_id::LocalDefId; |
| 10 | +use rustc_middle::ty::subst::{GenericArg, InternalSubsts}; |
| 11 | +use rustc_middle::ty::{self, ImplPolarity, Ty, TyCtxt, TypeFoldable, TypeVisitor}; |
| 12 | +use rustc_session::lint; |
| 13 | +use rustc_span::def_id::{DefId, LocalDefId}; |
10 | 14 | use rustc_span::Span;
|
11 | 15 | use rustc_trait_selection::traits;
|
| 16 | +use std::ops::ControlFlow; |
12 | 17 |
|
13 | 18 | pub(super) fn orphan_check_crate(tcx: TyCtxt<'_>, (): ()) -> &[LocalDefId] {
|
14 | 19 | let mut errors = Vec::new();
|
15 |
| - for (_trait, impls_of_trait) in tcx.all_local_trait_impls(()) { |
| 20 | + for (&trait_def_id, impls_of_trait) in tcx.all_local_trait_impls(()) { |
16 | 21 | for &impl_of_trait in impls_of_trait {
|
17 | 22 | match orphan_check_impl(tcx, impl_of_trait) {
|
18 | 23 | Ok(()) => {}
|
19 | 24 | Err(ErrorReported) => errors.push(impl_of_trait),
|
20 | 25 | }
|
21 | 26 | }
|
| 27 | + |
| 28 | + if tcx.trait_is_auto(trait_def_id) { |
| 29 | + lint_auto_trait_impls(tcx, trait_def_id, impls_of_trait); |
| 30 | + } |
22 | 31 | }
|
23 | 32 | tcx.arena.alloc_slice(&errors)
|
24 | 33 | }
|
@@ -265,3 +274,201 @@ fn emit_orphan_check_error<'tcx>(
|
265 | 274 |
|
266 | 275 | Err(ErrorReported)
|
267 | 276 | }
|
| 277 | + |
| 278 | +#[derive(Default)] |
| 279 | +struct AreUniqueParamsVisitor { |
| 280 | + seen: GrowableBitSet<u32>, |
| 281 | +} |
| 282 | + |
| 283 | +#[derive(Copy, Clone)] |
| 284 | +enum NotUniqueParam<'tcx> { |
| 285 | + DuplicateParam(GenericArg<'tcx>), |
| 286 | + NotParam(GenericArg<'tcx>), |
| 287 | +} |
| 288 | + |
| 289 | +impl<'tcx> TypeVisitor<'tcx> for AreUniqueParamsVisitor { |
| 290 | + type BreakTy = NotUniqueParam<'tcx>; |
| 291 | + fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 292 | + match t.kind() { |
| 293 | + ty::Param(p) => { |
| 294 | + if self.seen.insert(p.index) { |
| 295 | + ControlFlow::CONTINUE |
| 296 | + } else { |
| 297 | + ControlFlow::Break(NotUniqueParam::DuplicateParam(t.into())) |
| 298 | + } |
| 299 | + } |
| 300 | + _ => ControlFlow::Break(NotUniqueParam::NotParam(t.into())), |
| 301 | + } |
| 302 | + } |
| 303 | + fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 304 | + match r { |
| 305 | + ty::ReEarlyBound(p) => { |
| 306 | + if self.seen.insert(p.index) { |
| 307 | + ControlFlow::CONTINUE |
| 308 | + } else { |
| 309 | + ControlFlow::Break(NotUniqueParam::DuplicateParam(r.into())) |
| 310 | + } |
| 311 | + } |
| 312 | + _ => ControlFlow::Break(NotUniqueParam::NotParam(r.into())), |
| 313 | + } |
| 314 | + } |
| 315 | + fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 316 | + match c.val { |
| 317 | + ty::ConstKind::Param(p) => { |
| 318 | + if self.seen.insert(p.index) { |
| 319 | + ControlFlow::CONTINUE |
| 320 | + } else { |
| 321 | + ControlFlow::Break(NotUniqueParam::DuplicateParam(c.into())) |
| 322 | + } |
| 323 | + } |
| 324 | + _ => ControlFlow::Break(NotUniqueParam::NotParam(c.into())), |
| 325 | + } |
| 326 | + } |
| 327 | +} |
| 328 | + |
| 329 | +/// Lint impls of auto traits if they are likely to have |
| 330 | +/// unsound or surprising effects on auto impls. |
| 331 | +fn lint_auto_trait_impls(tcx: TyCtxt<'_>, trait_def_id: DefId, impls: &[LocalDefId]) { |
| 332 | + let mut non_covering_impls = Vec::new(); |
| 333 | + for &impl_def_id in impls { |
| 334 | + let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap(); |
| 335 | + if trait_ref.references_error() { |
| 336 | + return; |
| 337 | + } |
| 338 | + |
| 339 | + if tcx.impl_polarity(impl_def_id) != ImplPolarity::Positive { |
| 340 | + return; |
| 341 | + } |
| 342 | + |
| 343 | + assert_eq!(trait_ref.substs.len(), 1); |
| 344 | + let self_ty = trait_ref.self_ty(); |
| 345 | + let (self_type_did, substs) = match self_ty.kind() { |
| 346 | + ty::Adt(def, substs) => (def.did, substs), |
| 347 | + _ => { |
| 348 | + // FIXME: should also lint for stuff like `&i32` but |
| 349 | + // considering that auto traits are unstable, that |
| 350 | + // isn't too important for now as this only affects |
| 351 | + // crates using `nightly`, and std. |
| 352 | + continue; |
| 353 | + } |
| 354 | + }; |
| 355 | + |
| 356 | + // Impls which completely cover a given root type are fine as they |
| 357 | + // disable auto impls entirely. So only lint if the substs |
| 358 | + // are not a permutation of the identity substs. |
| 359 | + match substs.visit_with(&mut AreUniqueParamsVisitor::default()) { |
| 360 | + ControlFlow::Continue(()) => {} // ok |
| 361 | + ControlFlow::Break(arg) => { |
| 362 | + // Ideally: |
| 363 | + // |
| 364 | + // - compute the requirements for the auto impl candidate |
| 365 | + // - check whether these are implied by the non covering impls |
| 366 | + // - if not, emit the lint |
| 367 | + // |
| 368 | + // What we do here is a bit simpler: |
| 369 | + // |
| 370 | + // - badly check if an auto impl candidate definitely does not apply |
| 371 | + // for the given simplified type |
| 372 | + // - if so, do not lint |
| 373 | + if fast_reject_auto_impl(tcx, trait_def_id, self_ty) { |
| 374 | + // ok |
| 375 | + } else { |
| 376 | + non_covering_impls.push((impl_def_id, self_type_did, arg)); |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + for &(impl_def_id, self_type_did, arg) in &non_covering_impls { |
| 383 | + tcx.struct_span_lint_hir( |
| 384 | + lint::builtin::SUSPICIOUS_AUTO_TRAIT_IMPLS, |
| 385 | + tcx.hir().local_def_id_to_hir_id(impl_def_id), |
| 386 | + tcx.def_span(impl_def_id), |
| 387 | + |err| { |
| 388 | + let mut err = err.build(&format!( |
| 389 | + "cross-crate traits with a default impl, like `{}`, \ |
| 390 | + should not be specialized", |
| 391 | + tcx.def_path_str(trait_def_id), |
| 392 | + )); |
| 393 | + let item_span = tcx.def_span(self_type_did); |
| 394 | + let self_descr = tcx.def_kind(self_type_did).descr(self_type_did); |
| 395 | + err.span_note( |
| 396 | + item_span, |
| 397 | + &format!( |
| 398 | + "try using the same sequence of generic parameters as the {} definition", |
| 399 | + self_descr, |
| 400 | + ), |
| 401 | + ); |
| 402 | + match arg { |
| 403 | + NotUniqueParam::DuplicateParam(arg) => { |
| 404 | + err.note(&format!("`{}` is mentioned multiple times", arg)); |
| 405 | + } |
| 406 | + NotUniqueParam::NotParam(arg) => { |
| 407 | + err.note(&format!("`{}` is not a generic parameter", arg)); |
| 408 | + } |
| 409 | + } |
| 410 | + err.emit(); |
| 411 | + }, |
| 412 | + ); |
| 413 | + } |
| 414 | +} |
| 415 | + |
| 416 | +fn fast_reject_auto_impl<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, self_ty: Ty<'tcx>) -> bool { |
| 417 | + struct DisableAutoTraitVisitor<'tcx> { |
| 418 | + tcx: TyCtxt<'tcx>, |
| 419 | + trait_def_id: DefId, |
| 420 | + self_ty_root: Ty<'tcx>, |
| 421 | + seen: FxHashSet<DefId>, |
| 422 | + } |
| 423 | + |
| 424 | + impl<'tcx> TypeVisitor<'tcx> for DisableAutoTraitVisitor<'tcx> { |
| 425 | + type BreakTy = (); |
| 426 | + fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 427 | + let tcx = self.tcx; |
| 428 | + if t != self.self_ty_root { |
| 429 | + for impl_def_id in tcx.non_blanket_impls_for_ty(self.trait_def_id, t) { |
| 430 | + match tcx.impl_polarity(impl_def_id) { |
| 431 | + ImplPolarity::Negative => return ControlFlow::BREAK, |
| 432 | + ImplPolarity::Reservation => {} |
| 433 | + // FIXME(@lcnr): That's probably not good enough, idk |
| 434 | + // |
| 435 | + // We might just want to take the rustdoc code and somehow avoid |
| 436 | + // explicit impls for `Self`. |
| 437 | + ImplPolarity::Positive => return ControlFlow::CONTINUE, |
| 438 | + } |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + match t.kind() { |
| 443 | + ty::Adt(def, substs) => { |
| 444 | + // @lcnr: This is the only place where cycles can happen. We avoid this |
| 445 | + // by only visiting each `DefId` once. |
| 446 | + // |
| 447 | + // This will be is incorrect in subtle cases, but I don't care :) |
| 448 | + if self.seen.insert(def.did) { |
| 449 | + for ty in def.all_fields().map(|field| field.ty(tcx, substs)) { |
| 450 | + ty.visit_with(self)?; |
| 451 | + } |
| 452 | + } |
| 453 | + |
| 454 | + ControlFlow::CONTINUE |
| 455 | + } |
| 456 | + _ => t.super_visit_with(self), |
| 457 | + } |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + let self_ty_root = match self_ty.kind() { |
| 462 | + ty::Adt(def, _) => tcx.mk_adt(def, InternalSubsts::identity_for_item(tcx, def.did)), |
| 463 | + _ => unimplemented!("unexpected self ty {:?}", self_ty), |
| 464 | + }; |
| 465 | + |
| 466 | + self_ty_root |
| 467 | + .visit_with(&mut DisableAutoTraitVisitor { |
| 468 | + tcx, |
| 469 | + self_ty_root, |
| 470 | + trait_def_id, |
| 471 | + seen: FxHashSet::default(), |
| 472 | + }) |
| 473 | + .is_break() |
| 474 | +} |
0 commit comments