diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index f7d35f3ff3b4b..61a2dbc2f5fc2 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -3536,17 +3536,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { && let ty::Adt(adt_def, _) = return_ty.kind() && adt_def.did() == cow_did { - if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) { - if let Some(pos) = snippet.rfind(".to_owned") { - let byte_pos = BytePos(pos as u32 + 1u32); - let to_owned_span = return_span.with_hi(return_span.lo() + byte_pos); - err.span_suggestion_short( - to_owned_span.shrink_to_hi(), - "try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`", - "in", - Applicability::MaybeIncorrect, - ); - } + let typeck = tcx.typeck(self.mir_def_id()); + if let Some(expr) = self.find_expr(return_span) + && let Some(def_id) = typeck.type_dependent_def_id(expr.hir_id) + && tcx.is_diagnostic_item(sym::to_owned_method, def_id) + && let Some(to_owned_ident) = expr.method_ident() + { + err.span_suggestion_short( + to_owned_ident.span.shrink_to_lo(), + "try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`", + "in", + Applicability::MaybeIncorrect, + ); } } } diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index ab31ed61c123f..804d2757cf998 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -23,6 +23,7 @@ use rustc_hir::lang_items::LangItem; use rustc_index::IndexVec; use rustc_infer::infer::NllRegionVariableOrigin; use rustc_macros::extension; +use rustc_middle::mir::RETURN_PLACE; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{ self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty, @@ -584,7 +585,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { /// see `DefiningTy` for details. fn defining_ty(&self) -> DefiningTy<'tcx> { let tcx = self.infcx.tcx; - let typeck_root_def_id = tcx.typeck_root_def_id_local(self.mir_def); match tcx.hir_body_owner_kind(self.mir_def) { BodyOwnerKind::Closure | BodyOwnerKind::Fn => { @@ -614,36 +614,40 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { } BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => { - let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id); - if self.mir_def == typeck_root_def_id { - let args = self.infcx.replace_free_regions_with_nll_infer_vars( - NllRegionVariableOrigin::FreeRegion, - identity_args, - ); - DefiningTy::Const(self.mir_def.to_def_id(), args) - } else { - // FIXME: this line creates a query dependency between borrowck and typeck. - // - // This is required for `AscribeUserType` canonical query, which will call - // `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes - // into borrowck, which is ICE #78174. - // - // As a workaround, inline consts have an additional generic param (`ty` - // below), so that `type_of(inline_const_def_id).args(args)` uses the - // proper type with NLL infer vars. - let ty = tcx - .typeck(self.mir_def) - .node_type(tcx.local_def_id_to_hir_id(self.mir_def)); - let args = InlineConstArgs::new( - tcx, - InlineConstArgsParts { parent_args: identity_args, ty }, - ) - .args; - let args = self.infcx.replace_free_regions_with_nll_infer_vars( - NllRegionVariableOrigin::FreeRegion, - args, - ); - DefiningTy::InlineConst(self.mir_def.to_def_id(), args) + match tcx.def_kind(self.mir_def) { + DefKind::InlineConst => { + // This is required for `AscribeUserType` canonical query, which will call + // `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes + // into borrowck, which is ICE #78174. + // + // As a workaround, inline consts have an additional generic param (`ty` + // below), so that `type_of(inline_const_def_id).substs(substs)` uses the + // proper type with NLL infer vars. + // + // Fetch the actual type from MIR, as `type_of` returns something useless + // like ``. + let body = tcx.mir_promoted(self.mir_def).0.borrow(); + let ty = body.local_decls[RETURN_PLACE].ty; + let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id()); + let parent_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id); + let args = + InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty }) + .args; + let args = self.infcx.replace_free_regions_with_nll_infer_vars( + NllRegionVariableOrigin::FreeRegion, + args, + ); + DefiningTy::InlineConst(self.mir_def.to_def_id(), args) + } + _ => { + let identity_args = + GenericArgs::identity_for_item(tcx, self.mir_def.to_def_id()); + let args = self.infcx.replace_free_regions_with_nll_infer_vars( + NllRegionVariableOrigin::FreeRegion, + identity_args, + ); + DefiningTy::Const(self.mir_def.to_def_id(), args) + } } } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 702c3c54101c3..8ff4efa0b9c61 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2047,6 +2047,7 @@ symbols! { thumb2, thumb_mode: "thumb-mode", tmm_reg, + to_owned_method, to_string, to_vec, tool_attributes, diff --git a/src/tools/clippy/clippy_utils/src/sym.rs b/src/tools/clippy/clippy_utils/src/sym.rs index 87aac25f5bd12..367ec897dae48 100644 --- a/src/tools/clippy/clippy_utils/src/sym.rs +++ b/src/tools/clippy/clippy_utils/src/sym.rs @@ -606,7 +606,6 @@ generate! { to_ne_bytes, to_os_string, to_owned, - to_owned_method, to_path_buf, to_string_method, to_uppercase, diff --git a/tests/ui/issues/issue-44247.rs b/tests/ui/associated-consts/assoc-const-and-type-same-name.rs similarity index 76% rename from tests/ui/issues/issue-44247.rs rename to tests/ui/associated-consts/assoc-const-and-type-same-name.rs index 1ddd5a6dc0b3f..a7304562f593d 100644 --- a/tests/ui/issues/issue-44247.rs +++ b/tests/ui/associated-consts/assoc-const-and-type-same-name.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/44247 + //@ check-pass #![allow(dead_code)] trait T { diff --git a/tests/ui/issues/issue-31267-additional.rs b/tests/ui/associated-consts/assoc-const-in-array-initializer.rs similarity index 78% rename from tests/ui/issues/issue-31267-additional.rs rename to tests/ui/associated-consts/assoc-const-in-array-initializer.rs index ef7a5002bf162..9787d8d677621 100644 --- a/tests/ui/issues/issue-31267-additional.rs +++ b/tests/ui/associated-consts/assoc-const-in-array-initializer.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/31267 + //@ run-pass #[derive(Clone, Copy, Debug)] diff --git a/tests/ui/issues/issue-24947.rs b/tests/ui/associated-consts/assoc-const-in-array-size.rs similarity index 75% rename from tests/ui/issues/issue-24947.rs rename to tests/ui/associated-consts/assoc-const-in-array-size.rs index c607cb7ec89d6..b6db21550a435 100644 --- a/tests/ui/issues/issue-24947.rs +++ b/tests/ui/associated-consts/assoc-const-in-array-size.rs @@ -1,6 +1,6 @@ //@ run-pass -// #24947 ICE using a trait-associated const in an array size - +// Regression test for https://github.com/rust-lang/rust/issues/24947 +// ICE using a trait-associated const in an array size struct Foo; diff --git a/tests/ui/issues/issue-25145.rs b/tests/ui/associated-consts/assoc-const-in-static-array-size.rs similarity index 66% rename from tests/ui/issues/issue-25145.rs rename to tests/ui/associated-consts/assoc-const-in-static-array-size.rs index 7edaa91d4a0cf..a7700f7c48610 100644 --- a/tests/ui/issues/issue-25145.rs +++ b/tests/ui/associated-consts/assoc-const-in-static-array-size.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/25145 + //@ run-pass struct S; diff --git a/tests/ui/issues/issue-42956.rs b/tests/ui/associated-consts/assoc-const-through-assoc-type-path.rs similarity index 81% rename from tests/ui/issues/issue-42956.rs rename to tests/ui/associated-consts/assoc-const-through-assoc-type-path.rs index 5d6d4249a7d88..5202bdd6d82f1 100644 --- a/tests/ui/issues/issue-42956.rs +++ b/tests/ui/associated-consts/assoc-const-through-assoc-type-path.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/42956 + //@ check-pass #![allow(dead_code)] diff --git a/tests/ui/issues/issue-28586.rs b/tests/ui/associated-consts/no-assoc-item-bytes-on-usize.rs similarity index 63% rename from tests/ui/issues/issue-28586.rs rename to tests/ui/associated-consts/no-assoc-item-bytes-on-usize.rs index 9520e0e51a15a..7b05a074c3672 100644 --- a/tests/ui/issues/issue-28586.rs +++ b/tests/ui/associated-consts/no-assoc-item-bytes-on-usize.rs @@ -1,4 +1,4 @@ -// Regression test for issue #28586 +// Regression test for issue https://github.com/rust-lang/rust/issues/28586 pub trait Foo {} impl Foo for [u8; usize::BYTES] {} diff --git a/tests/ui/issues/issue-28586.stderr b/tests/ui/associated-consts/no-assoc-item-bytes-on-usize.stderr similarity index 87% rename from tests/ui/issues/issue-28586.stderr rename to tests/ui/associated-consts/no-assoc-item-bytes-on-usize.stderr index dda147954facd..982d58cf0ac58 100644 --- a/tests/ui/issues/issue-28586.stderr +++ b/tests/ui/associated-consts/no-assoc-item-bytes-on-usize.stderr @@ -1,5 +1,5 @@ error[E0599]: no associated function or constant named `BYTES` found for type `usize` in the current scope - --> $DIR/issue-28586.rs:4:26 + --> $DIR/no-assoc-item-bytes-on-usize.rs:4:26 | LL | impl Foo for [u8; usize::BYTES] {} | ^^^^^ associated function or constant not found in `usize` diff --git a/tests/ui/issues/issue-34780.rs b/tests/ui/associated-consts/trait-const-with-phantom-data-lifetime.rs similarity index 65% rename from tests/ui/issues/issue-34780.rs rename to tests/ui/associated-consts/trait-const-with-phantom-data-lifetime.rs index 4470e3af682b0..16be49f299690 100644 --- a/tests/ui/issues/issue-34780.rs +++ b/tests/ui/associated-consts/trait-const-with-phantom-data-lifetime.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/34780 + //@ check-pass use std::marker::PhantomData; diff --git a/tests/ui/issues/issue-43988.rs b/tests/ui/attributes/attribute-on-wrong-item-inline-repr.rs similarity index 90% rename from tests/ui/issues/issue-43988.rs rename to tests/ui/attributes/attribute-on-wrong-item-inline-repr.rs index bd23e0e2457df..c29edbe14f244 100644 --- a/tests/ui/issues/issue-43988.rs +++ b/tests/ui/attributes/attribute-on-wrong-item-inline-repr.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/43988 + #![feature(stmt_expr_attributes)] fn main() { diff --git a/tests/ui/issues/issue-43988.stderr b/tests/ui/attributes/attribute-on-wrong-item-inline-repr.stderr similarity index 86% rename from tests/ui/issues/issue-43988.stderr rename to tests/ui/attributes/attribute-on-wrong-item-inline-repr.stderr index d393255e0ee1d..49f67bf6ad044 100644 --- a/tests/ui/issues/issue-43988.stderr +++ b/tests/ui/attributes/attribute-on-wrong-item-inline-repr.stderr @@ -1,5 +1,5 @@ error: `#[inline]` attribute cannot be used on statements - --> $DIR/issue-43988.rs:5:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:7:5 | LL | #[inline] | ^^^^^^^^^ @@ -7,7 +7,7 @@ LL | #[inline] = help: `#[inline]` can only be applied to functions error[E0539]: malformed `inline` attribute input - --> $DIR/issue-43988.rs:10:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:12:5 | LL | #[inline(XYZ)] | ^^^^^^^^^---^^ @@ -28,7 +28,7 @@ LL + #[inline] | error: `#[inline]` attribute cannot be used on statements - --> $DIR/issue-43988.rs:10:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:12:5 | LL | #[inline(XYZ)] | ^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL | #[inline(XYZ)] = help: `#[inline]` can only be applied to functions error[E0552]: unrecognized representation hint - --> $DIR/issue-43988.rs:15:12 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:17:12 | LL | #[repr(nothing)] | ^^^^^^^ @@ -45,7 +45,7 @@ LL | #[repr(nothing)] = note: for more information, visit error[E0552]: unrecognized representation hint - --> $DIR/issue-43988.rs:19:12 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:21:12 | LL | #[repr(something_not_real)] | ^^^^^^^^^^^^^^^^^^ @@ -54,7 +54,7 @@ LL | #[repr(something_not_real)] = note: for more information, visit error[E0539]: malformed `repr` attribute input - --> $DIR/issue-43988.rs:25:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:27:5 | LL | #[repr] | ^^^^^^^ expected this to be a list @@ -62,7 +62,7 @@ LL | #[repr] = note: for more information, visit error[E0539]: malformed `inline` attribute input - --> $DIR/issue-43988.rs:31:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:33:5 | LL | #[inline(ABC)] | ^^^^^^^^^---^^ @@ -83,7 +83,7 @@ LL + #[inline] | error: `#[inline]` attribute cannot be used on expressions - --> $DIR/issue-43988.rs:31:5 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:33:5 | LL | #[inline(ABC)] | ^^^^^^^^^^^^^^ @@ -91,7 +91,7 @@ LL | #[inline(ABC)] = help: `#[inline]` can only be applied to functions error[E0539]: malformed `repr` attribute input - --> $DIR/issue-43988.rs:36:14 + --> $DIR/attribute-on-wrong-item-inline-repr.rs:38:14 | LL | let _z = #[repr] 1; | ^^^^^^^ expected this to be a list diff --git a/tests/ui/issues/issue-24434.rs b/tests/ui/attributes/cfg-attr-feature-gate-and-rustc-dummy.rs similarity index 54% rename from tests/ui/issues/issue-24434.rs rename to tests/ui/attributes/cfg-attr-feature-gate-and-rustc-dummy.rs index 429bcf4a8d87d..c7f56494f8ee8 100644 --- a/tests/ui/issues/issue-24434.rs +++ b/tests/ui/attributes/cfg-attr-feature-gate-and-rustc-dummy.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/24434 + //@ check-pass #![cfg_attr(true, feature(rustc_attrs))] diff --git a/tests/ui/issues/issue-49632.rs b/tests/ui/attributes/inline-attribute-on-closure.rs similarity index 70% rename from tests/ui/issues/issue-49632.rs rename to tests/ui/attributes/inline-attribute-on-closure.rs index f17891c45013f..981d58e11e466 100644 --- a/tests/ui/issues/issue-49632.rs +++ b/tests/ui/attributes/inline-attribute-on-closure.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/49632 + //@ run-pass #![feature(stmt_expr_attributes)] diff --git a/tests/ui/issues/issue-45562.fixed b/tests/ui/attributes/no-mangle-on-const-error.fixed similarity index 68% rename from tests/ui/issues/issue-45562.fixed rename to tests/ui/attributes/no-mangle-on-const-error.fixed index 529b5bd744e03..0612385521dde 100644 --- a/tests/ui/issues/issue-45562.fixed +++ b/tests/ui/attributes/no-mangle-on-const-error.fixed @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/45562 + //@ run-rustfix #![deny(unused_attributes)] diff --git a/tests/ui/issues/issue-45562.rs b/tests/ui/attributes/no-mangle-on-const-error.rs similarity index 68% rename from tests/ui/issues/issue-45562.rs rename to tests/ui/attributes/no-mangle-on-const-error.rs index 7c30a967c7848..0366ce2e4b231 100644 --- a/tests/ui/issues/issue-45562.rs +++ b/tests/ui/attributes/no-mangle-on-const-error.rs @@ -1,3 +1,5 @@ +//! Regression test for https://github.com/rust-lang/rust/issues/45562 + //@ run-rustfix #![deny(unused_attributes)] diff --git a/tests/ui/issues/issue-45562.stderr b/tests/ui/attributes/no-mangle-on-const-error.stderr similarity index 88% rename from tests/ui/issues/issue-45562.stderr rename to tests/ui/attributes/no-mangle-on-const-error.stderr index 3372986af9373..af46ea8a5b9e2 100644 --- a/tests/ui/issues/issue-45562.stderr +++ b/tests/ui/attributes/no-mangle-on-const-error.stderr @@ -1,5 +1,5 @@ error: const items should never be `#[no_mangle]` - --> $DIR/issue-45562.rs:5:14 + --> $DIR/no-mangle-on-const-error.rs:7:14 | LL | #[no_mangle] pub const RAH: usize = 5; | ----------^^^^^^^^^^^^^^^