Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
}
Expand Down
66 changes: 35 additions & 31 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 `<const_ty>`.
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)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,7 @@ symbols! {
thumb2,
thumb_mode: "thumb-mode",
tmm_reg,
to_owned_method,
to_string,
to_vec,
tool_attributes,
Expand Down
1 change: 0 additions & 1 deletion src/tools/clippy/clippy_utils/src/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ generate! {
to_ne_bytes,
to_os_string,
to_owned,
to_owned_method,
to_path_buf,
to_string_method,
to_uppercase,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/44247

//@ check-pass
#![allow(dead_code)]
trait T {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/31267

//@ run-pass

#[derive(Clone, Copy, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/25145

//@ run-pass

struct S;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/42956

//@ check-pass
#![allow(dead_code)]

Expand Down
Original file line number Diff line number Diff line change
@@ -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] {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/34780

//@ check-pass

use std::marker::PhantomData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/43988

#![feature(stmt_expr_attributes)]

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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]
| ^^^^^^^^^
|
= 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)]
| ^^^^^^^^^---^^
Expand All @@ -28,15 +28,15 @@ 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)]
| ^^^^^^^^^^^^^^
|
= 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)]
| ^^^^^^^
Expand All @@ -45,7 +45,7 @@ LL | #[repr(nothing)]
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html?highlight=repr#representations>

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)]
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -54,15 +54,15 @@ LL | #[repr(something_not_real)]
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html?highlight=repr#representations>

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
|
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>

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)]
| ^^^^^^^^^---^^
Expand All @@ -83,15 +83,15 @@ 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)]
| ^^^^^^^^^^^^^^
|
= 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/24434

//@ check-pass

#![cfg_attr(true, feature(rustc_attrs))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/49632

//@ run-pass
#![feature(stmt_expr_attributes)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/45562

//@ run-rustfix

#![deny(unused_attributes)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Regression test for https://github.com/rust-lang/rust/issues/45562

//@ run-rustfix

#![deny(unused_attributes)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
| ----------^^^^^^^^^^^^^^^
Expand Down
Loading