Skip to content

Commit 3d5dfdc

Browse files
committed
Auto merge of #156088 - JonathanBrouwer:rollup-qdmx6JQ, r=JonathanBrouwer
Rollup of 4 pull requests Successful merges: - #156074 (Add extra symbol check for `.to_owned()`) - #156045 (Move tests associated consts) - #156064 (Do not depend on typeck to borrow-check inline consts.) - #156083 (Move tests attributes)
2 parents 696d592 + dd3cb25 commit 3d5dfdc

19 files changed

Lines changed: 82 additions & 57 deletions

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,17 +3535,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
35353535
&& let ty::Adt(adt_def, _) = return_ty.kind()
35363536
&& adt_def.did() == cow_did
35373537
{
3538-
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) {
3539-
if let Some(pos) = snippet.rfind(".to_owned") {
3540-
let byte_pos = BytePos(pos as u32 + 1u32);
3541-
let to_owned_span = return_span.with_hi(return_span.lo() + byte_pos);
3542-
err.span_suggestion_short(
3543-
to_owned_span.shrink_to_hi(),
3544-
"try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`",
3545-
"in",
3546-
Applicability::MaybeIncorrect,
3547-
);
3548-
}
3538+
let typeck = tcx.typeck(self.mir_def_id());
3539+
if let Some(expr) = self.find_expr(return_span)
3540+
&& let Some(def_id) = typeck.type_dependent_def_id(expr.hir_id)
3541+
&& tcx.is_diagnostic_item(sym::to_owned_method, def_id)
3542+
&& let Some(to_owned_ident) = expr.method_ident()
3543+
{
3544+
err.span_suggestion_short(
3545+
to_owned_ident.span.shrink_to_lo(),
3546+
"try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`",
3547+
"in",
3548+
Applicability::MaybeIncorrect,
3549+
);
35493550
}
35503551
}
35513552
}

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_hir::lang_items::LangItem;
2323
use rustc_index::IndexVec;
2424
use rustc_infer::infer::NllRegionVariableOrigin;
2525
use rustc_macros::extension;
26+
use rustc_middle::mir::RETURN_PLACE;
2627
use rustc_middle::ty::print::with_no_trimmed_paths;
2728
use rustc_middle::ty::{
2829
self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty,
@@ -584,7 +585,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
584585
/// see `DefiningTy` for details.
585586
fn defining_ty(&self) -> DefiningTy<'tcx> {
586587
let tcx = self.infcx.tcx;
587-
let typeck_root_def_id = tcx.typeck_root_def_id_local(self.mir_def);
588588

589589
match tcx.hir_body_owner_kind(self.mir_def) {
590590
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
@@ -614,36 +614,40 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
614614
}
615615

616616
BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => {
617-
let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
618-
if self.mir_def == typeck_root_def_id {
619-
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
620-
NllRegionVariableOrigin::FreeRegion,
621-
identity_args,
622-
);
623-
DefiningTy::Const(self.mir_def.to_def_id(), args)
624-
} else {
625-
// FIXME: this line creates a query dependency between borrowck and typeck.
626-
//
627-
// This is required for `AscribeUserType` canonical query, which will call
628-
// `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes
629-
// into borrowck, which is ICE #78174.
630-
//
631-
// As a workaround, inline consts have an additional generic param (`ty`
632-
// below), so that `type_of(inline_const_def_id).args(args)` uses the
633-
// proper type with NLL infer vars.
634-
let ty = tcx
635-
.typeck(self.mir_def)
636-
.node_type(tcx.local_def_id_to_hir_id(self.mir_def));
637-
let args = InlineConstArgs::new(
638-
tcx,
639-
InlineConstArgsParts { parent_args: identity_args, ty },
640-
)
641-
.args;
642-
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
643-
NllRegionVariableOrigin::FreeRegion,
644-
args,
645-
);
646-
DefiningTy::InlineConst(self.mir_def.to_def_id(), args)
617+
match tcx.def_kind(self.mir_def) {
618+
DefKind::InlineConst => {
619+
// This is required for `AscribeUserType` canonical query, which will call
620+
// `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes
621+
// into borrowck, which is ICE #78174.
622+
//
623+
// As a workaround, inline consts have an additional generic param (`ty`
624+
// below), so that `type_of(inline_const_def_id).substs(substs)` uses the
625+
// proper type with NLL infer vars.
626+
//
627+
// Fetch the actual type from MIR, as `type_of` returns something useless
628+
// like `<const_ty>`.
629+
let body = tcx.mir_promoted(self.mir_def).0.borrow();
630+
let ty = body.local_decls[RETURN_PLACE].ty;
631+
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
632+
let parent_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
633+
let args =
634+
InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty })
635+
.args;
636+
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
637+
NllRegionVariableOrigin::FreeRegion,
638+
args,
639+
);
640+
DefiningTy::InlineConst(self.mir_def.to_def_id(), args)
641+
}
642+
_ => {
643+
let identity_args =
644+
GenericArgs::identity_for_item(tcx, self.mir_def.to_def_id());
645+
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
646+
NllRegionVariableOrigin::FreeRegion,
647+
identity_args,
648+
);
649+
DefiningTy::Const(self.mir_def.to_def_id(), args)
650+
}
647651
}
648652
}
649653

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ symbols! {
20472047
thumb2,
20482048
thumb_mode: "thumb-mode",
20492049
tmm_reg,
2050+
to_owned_method,
20502051
to_string,
20512052
to_vec,
20522053
tool_attributes,

src/tools/clippy/clippy_utils/src/sym.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@ generate! {
606606
to_ne_bytes,
607607
to_os_string,
608608
to_owned,
609-
to_owned_method,
610609
to_path_buf,
611610
to_string_method,
612611
to_uppercase,

tests/ui/issues/issue-44247.rs renamed to tests/ui/associated-consts/assoc-const-and-type-same-name.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Regression test for https://github.com/rust-lang/rust/issues/44247
2+
13
//@ check-pass
24
#![allow(dead_code)]
35
trait T {

tests/ui/issues/issue-31267-additional.rs renamed to tests/ui/associated-consts/assoc-const-in-array-initializer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Regression test for https://github.com/rust-lang/rust/issues/31267
2+
13
//@ run-pass
24

35
#[derive(Clone, Copy, Debug)]

tests/ui/issues/issue-24947.rs renamed to tests/ui/associated-consts/assoc-const-in-array-size.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ run-pass
2-
// #24947 ICE using a trait-associated const in an array size
3-
2+
// Regression test for https://github.com/rust-lang/rust/issues/24947
3+
// ICE using a trait-associated const in an array size
44

55
struct Foo;
66

tests/ui/issues/issue-25145.rs renamed to tests/ui/associated-consts/assoc-const-in-static-array-size.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Regression test for https://github.com/rust-lang/rust/issues/25145
2+
13
//@ run-pass
24

35
struct S;

tests/ui/issues/issue-42956.rs renamed to tests/ui/associated-consts/assoc-const-through-assoc-type-path.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Regression test for https://github.com/rust-lang/rust/issues/42956
2+
13
//@ check-pass
24
#![allow(dead_code)]
35

tests/ui/issues/issue-28586.rs renamed to tests/ui/associated-consts/no-assoc-item-bytes-on-usize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Regression test for issue #28586
1+
// Regression test for issue https://github.com/rust-lang/rust/issues/28586
22

33
pub trait Foo {}
44
impl Foo for [u8; usize::BYTES] {}

0 commit comments

Comments
 (0)