Skip to content

Commit 7ac9a3a

Browse files
committed
Auto merge of rust-lang#117193 - matthiaskrgr:rollup-bygfdcd, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#116401 (Return multiple object-safety violation errors and code improvements to the object-safety check) - rust-lang#116553 (Do not suggest 'Trait<Assoc=arg>' when in trait impl) - rust-lang#116931 (Improve the warning messages for the `#[diagnostic::on_unimplemented]`) - rust-lang#117008 (Uplift `Canonical` to `rustc_type_ir`) - rust-lang#117009 (On unresolved imports, suggest a disambiguated path if necessary to avoid collision with local items) - rust-lang#117175 (Rename AsyncCoroutineKind to CoroutineSource) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ab5c841 + 4e4e561 commit 7ac9a3a

File tree

59 files changed

+857
-320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+857
-320
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188
e.id,
189189
None,
190190
e.span,
191-
hir::AsyncCoroutineKind::Block,
191+
hir::CoroutineSource::Block,
192192
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
193193
),
194194
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
@@ -598,7 +598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
598598
closure_node_id: NodeId,
599599
ret_ty: Option<hir::FnRetTy<'hir>>,
600600
span: Span,
601-
async_gen_kind: hir::AsyncCoroutineKind,
601+
async_gen_kind: hir::CoroutineSource,
602602
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
603603
) -> hir::ExprKind<'hir> {
604604
let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
@@ -1005,7 +1005,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10051005
inner_closure_id,
10061006
async_ret_ty,
10071007
body.span,
1008-
hir::AsyncCoroutineKind::Closure,
1008+
hir::CoroutineSource::Closure,
10091009
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
10101010
);
10111011
let hir_id = this.lower_node_id(inner_closure_id);

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12061206
closure_id,
12071207
None,
12081208
body.span,
1209-
hir::AsyncCoroutineKind::Fn,
1209+
hir::CoroutineSource::Fn,
12101210
|this| {
12111211
// Create a block from the user's function body:
12121212
let user_body = this.lower_block_expr(body);

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{
88
use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
11-
use rustc_hir::{AsyncCoroutineKind, CoroutineKind, LangItem};
11+
use rustc_hir::{CoroutineKind, CoroutineSource, LangItem};
1212
use rustc_infer::traits::ObligationCause;
1313
use rustc_middle::hir::nested_filter::OnlyBodies;
1414
use rustc_middle::mir::tcx::PlaceTy;
@@ -2506,8 +2506,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25062506
let kind = match use_span.coroutine_kind() {
25072507
Some(coroutine_kind) => match coroutine_kind {
25082508
CoroutineKind::Async(async_kind) => match async_kind {
2509-
AsyncCoroutineKind::Block => "async block",
2510-
AsyncCoroutineKind::Closure => "async closure",
2509+
CoroutineSource::Block => "async block",
2510+
CoroutineSource::Closure => "async closure",
25112511
_ => bug!("async block/closure expected, but async function found."),
25122512
},
25132513
CoroutineKind::Coroutine => "coroutine",

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
682682
};
683683
let mir_description = match hir.body(body).coroutine_kind {
684684
Some(hir::CoroutineKind::Async(gen)) => match gen {
685-
hir::AsyncCoroutineKind::Block => " of async block",
686-
hir::AsyncCoroutineKind::Closure => " of async closure",
687-
hir::AsyncCoroutineKind::Fn => {
685+
hir::CoroutineSource::Block => " of async block",
686+
hir::CoroutineSource::Closure => " of async closure",
687+
hir::CoroutineSource::Fn => {
688688
let parent_item =
689689
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
690690
let output = &parent_item

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_data_structures::fx::FxHashSet;
1515
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
1616
use rustc_hir::def_id::DefId;
1717
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
18-
use rustc_hir::{AsyncCoroutineKind, CoroutineKind, Mutability};
18+
use rustc_hir::{CoroutineKind, CoroutineSource, Mutability};
1919
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
2020
use rustc_middle::ty::{self, ExistentialProjection, ParamEnv, Ty, TyCtxt};
2121
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
@@ -560,9 +560,9 @@ pub fn push_item_name(tcx: TyCtxt<'_>, def_id: DefId, qualified: bool, output: &
560560

561561
fn coroutine_kind_label(coroutine_kind: Option<CoroutineKind>) -> &'static str {
562562
match coroutine_kind {
563-
Some(CoroutineKind::Async(AsyncCoroutineKind::Block)) => "async_block",
564-
Some(CoroutineKind::Async(AsyncCoroutineKind::Closure)) => "async_closure",
565-
Some(CoroutineKind::Async(AsyncCoroutineKind::Fn)) => "async_fn",
563+
Some(CoroutineKind::Async(CoroutineSource::Block)) => "async_block",
564+
Some(CoroutineKind::Async(CoroutineSource::Closure)) => "async_closure",
565+
Some(CoroutineKind::Async(CoroutineSource::Fn)) => "async_fn",
566566
Some(CoroutineKind::Coroutine) => "coroutine",
567567
None => "closure",
568568
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
360360
pub struct Coroutine(pub hir::CoroutineKind);
361361
impl<'tcx> NonConstOp<'tcx> for Coroutine {
362362
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
363-
if let hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) = self.0 {
363+
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
364364
Status::Unstable(sym::const_async_blocks)
365365
} else {
366366
Status::Forbidden
@@ -372,8 +372,8 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
372372
ccx: &ConstCx<'_, 'tcx>,
373373
span: Span,
374374
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
375-
let msg = format!("{}s are not allowed in {}s", self.0.descr(), ccx.const_kind());
376-
if let hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) = self.0 {
375+
let msg = format!("{:#}s are not allowed in {}s", self.0, ccx.const_kind());
376+
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
377377
ccx.tcx.sess.create_feature_err(
378378
errors::UnallowedOpInConstContext { span, msg },
379379
sym::const_async_blocks,

compiler/rustc_hir/src/hir.rs

+20-31
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ impl<'hir> Body<'hir> {
15111511
#[derive(HashStable_Generic, Encodable, Decodable)]
15121512
pub enum CoroutineKind {
15131513
/// An explicit `async` block or the body of an async function.
1514-
Async(AsyncCoroutineKind),
1514+
Async(CoroutineSource),
15151515

15161516
/// A coroutine literal created via a `yield` inside a closure.
15171517
Coroutine,
@@ -1520,56 +1520,45 @@ pub enum CoroutineKind {
15201520
impl fmt::Display for CoroutineKind {
15211521
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15221522
match self {
1523-
CoroutineKind::Async(k) => fmt::Display::fmt(k, f),
1523+
CoroutineKind::Async(k) => {
1524+
if f.alternate() {
1525+
f.write_str("`async` ")?;
1526+
} else {
1527+
f.write_str("async ")?
1528+
}
1529+
k.fmt(f)
1530+
}
15241531
CoroutineKind::Coroutine => f.write_str("coroutine"),
15251532
}
15261533
}
15271534
}
15281535

1529-
impl CoroutineKind {
1530-
pub fn descr(&self) -> &'static str {
1531-
match self {
1532-
CoroutineKind::Async(ask) => ask.descr(),
1533-
CoroutineKind::Coroutine => "coroutine",
1534-
}
1535-
}
1536-
}
1537-
1538-
/// In the case of a coroutine created as part of an async construct,
1539-
/// which kind of async construct caused it to be created?
1536+
/// In the case of a coroutine created as part of an async/gen construct,
1537+
/// which kind of async/gen construct caused it to be created?
15401538
///
15411539
/// This helps error messages but is also used to drive coercions in
15421540
/// type-checking (see #60424).
15431541
#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
15441542
#[derive(HashStable_Generic, Encodable, Decodable)]
1545-
pub enum AsyncCoroutineKind {
1546-
/// An explicit `async` block written by the user.
1543+
pub enum CoroutineSource {
1544+
/// An explicit `async`/`gen` block written by the user.
15471545
Block,
15481546

1549-
/// An explicit `async` closure written by the user.
1547+
/// An explicit `async`/`gen` closure written by the user.
15501548
Closure,
15511549

1552-
/// The `async` block generated as the body of an async function.
1550+
/// The `async`/`gen` block generated as the body of an async/gen function.
15531551
Fn,
15541552
}
15551553

1556-
impl fmt::Display for AsyncCoroutineKind {
1554+
impl fmt::Display for CoroutineSource {
15571555
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1558-
f.write_str(match self {
1559-
AsyncCoroutineKind::Block => "async block",
1560-
AsyncCoroutineKind::Closure => "async closure body",
1561-
AsyncCoroutineKind::Fn => "async fn body",
1562-
})
1563-
}
1564-
}
1565-
1566-
impl AsyncCoroutineKind {
1567-
pub fn descr(&self) -> &'static str {
15681556
match self {
1569-
AsyncCoroutineKind::Block => "`async` block",
1570-
AsyncCoroutineKind::Closure => "`async` closure body",
1571-
AsyncCoroutineKind::Fn => "`async fn` body",
1557+
CoroutineSource::Block => "block",
1558+
CoroutineSource::Closure => "closure body",
1559+
CoroutineSource::Fn => "fn body",
15721560
}
1561+
.fmt(f)
15731562
}
15741563
}
15751564

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+57-13
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,44 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
129129
if self.missing_lifetimes() { "lifetime" } else { "generic" }
130130
}
131131

132+
/// Returns true if the generic type is a trait
133+
/// and is being referred to from one of its trait impls
134+
fn is_in_trait_impl(&self) -> bool {
135+
if self.tcx.is_trait(self.def_id) {
136+
// Here we check if the reference to the generic type
137+
// is from the 'of_trait' field of the enclosing impl
138+
139+
let parent = self.tcx.hir().get_parent(self.path_segment.hir_id);
140+
let parent_item = self
141+
.tcx
142+
.hir()
143+
.get_by_def_id(self.tcx.hir().get_parent_item(self.path_segment.hir_id).def_id);
144+
145+
// Get the HIR id of the trait ref
146+
let hir::Node::TraitRef(hir::TraitRef { hir_ref_id: trait_ref_id, .. }) = parent else {
147+
return false;
148+
};
149+
150+
// Get the HIR id of the 'of_trait' field of the impl
151+
let hir::Node::Item(hir::Item {
152+
kind:
153+
hir::ItemKind::Impl(hir::Impl {
154+
of_trait: Some(hir::TraitRef { hir_ref_id: id_in_of_trait, .. }),
155+
..
156+
}),
157+
..
158+
}) = parent_item
159+
else {
160+
return false;
161+
};
162+
163+
// Check that trait is referred to from the of_trait field of impl
164+
trait_ref_id == id_in_of_trait
165+
} else {
166+
false
167+
}
168+
}
169+
132170
fn num_provided_args(&self) -> usize {
133171
if self.missing_lifetimes() {
134172
self.num_provided_lifetime_args()
@@ -955,20 +993,26 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
955993
// If there is a single unbound associated type and a single excess generic param
956994
// suggest replacing the generic param with the associated type bound
957995
if provided_args_matches_unbound_traits && !unbound_types.is_empty() {
958-
let unused_generics = &self.gen_args.args[self.num_expected_type_or_const_args()..];
959-
let suggestions = iter::zip(unused_generics, &unbound_types)
960-
.map(|(potential, name)| (potential.span().shrink_to_lo(), format!("{name} = ")))
961-
.collect::<Vec<_>>();
996+
// Don't suggest if we're in a trait impl as
997+
// that would result in invalid syntax (fixes #116464)
998+
if !self.is_in_trait_impl() {
999+
let unused_generics = &self.gen_args.args[self.num_expected_type_or_const_args()..];
1000+
let suggestions = iter::zip(unused_generics, &unbound_types)
1001+
.map(|(potential, name)| {
1002+
(potential.span().shrink_to_lo(), format!("{name} = "))
1003+
})
1004+
.collect::<Vec<_>>();
9621005

963-
if !suggestions.is_empty() {
964-
err.multipart_suggestion_verbose(
965-
format!(
966-
"replace the generic bound{s} with the associated type{s}",
967-
s = pluralize!(unbound_types.len())
968-
),
969-
suggestions,
970-
Applicability::MaybeIncorrect,
971-
);
1006+
if !suggestions.is_empty() {
1007+
err.multipart_suggestion_verbose(
1008+
format!(
1009+
"replace the generic bound{s} with the associated type{s}",
1010+
s = pluralize!(unbound_types.len())
1011+
),
1012+
suggestions,
1013+
Applicability::MaybeIncorrect,
1014+
);
1015+
}
9721016
}
9731017
} else if remove_entire_generics {
9741018
let span = self

compiler/rustc_hir_typeck/src/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
305305
) = (parent_node, callee_node)
306306
{
307307
let fn_decl_span = if hir.body(body).coroutine_kind
308-
== Some(hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Closure))
308+
== Some(hir::CoroutineKind::Async(hir::CoroutineSource::Closure))
309309
{
310310
// Actually need to unwrap one more layer of HIR to get to
311311
// the _real_ closure...

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
636636
// In the case of the async block that we create for a function body,
637637
// we expect the return type of the block to match that of the enclosing
638638
// function.
639-
Some(hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Fn)) => {
639+
Some(hir::CoroutineKind::Async(hir::CoroutineSource::Fn)) => {
640640
debug!("closure is async fn body");
641641
let def_id = self.tcx.hir().body_owner_def_id(body.id());
642642
self.deduce_future_output_from_obligations(expr_def_id, def_id).unwrap_or_else(

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::ty::error::TypeError;
2626
use rustc_middle::ty::fold::TypeFoldable;
2727
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
2828
use rustc_middle::ty::{
29-
self, AdtKind, CanonicalUserType, GenericParamDefKind, Ty, TyCtxt, UserType,
29+
self, AdtKind, CanonicalUserType, GenericParamDefKind, IsIdentity, Ty, TyCtxt, UserType,
3030
};
3131
use rustc_middle::ty::{GenericArgKind, GenericArgsRef, UserArgs, UserSelfTy};
3232
use rustc_session::lint;
@@ -207,6 +207,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207207
) {
208208
debug!("fcx {}", self.tag());
209209

210+
// FIXME: is_identity being on `UserType` and not `Canonical<UserType>` is awkward
210211
if !canonical_user_type_annotation.is_identity() {
211212
self.typeck_results
212213
.borrow_mut()

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_hir::def::Res;
1010
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
1111
use rustc_hir::lang_items::LangItem;
1212
use rustc_hir::{
13-
AsyncCoroutineKind, CoroutineKind, Expr, ExprKind, GenericBound, HirId, Node, Path, QPath,
14-
Stmt, StmtKind, TyKind, WherePredicate,
13+
CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node, Path, QPath, Stmt,
14+
StmtKind, TyKind, WherePredicate,
1515
};
1616
use rustc_hir_analysis::astconv::AstConv;
1717
use rustc_infer::traits::{self, StatementAsExpression};
@@ -536,7 +536,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
536536
ty::Coroutine(def_id, ..)
537537
if matches!(
538538
self.tcx.coroutine_kind(def_id),
539-
Some(CoroutineKind::Async(AsyncCoroutineKind::Closure))
539+
Some(CoroutineKind::Async(CoroutineSource::Closure))
540540
) =>
541541
{
542542
errors::SuggestBoxing::AsyncBody

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1584,14 +1584,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15841584
target: &str,
15851585
types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
15861586
) {
1587-
for (key, values) in types.iter() {
1587+
for (kind, values) in types.iter() {
15881588
let count = values.len();
1589-
let kind = key.descr();
15901589
for &sp in values {
15911590
err.span_label(
15921591
sp,
15931592
format!(
1594-
"{}{} {}{}",
1593+
"{}{} {:#}{}",
15951594
if count == 1 { "the " } else { "one of the " },
15961595
target,
15971596
kind,
@@ -2952,17 +2951,19 @@ pub enum TyCategory {
29522951
Foreign,
29532952
}
29542953

2955-
impl TyCategory {
2956-
fn descr(&self) -> &'static str {
2954+
impl fmt::Display for TyCategory {
2955+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29572956
match self {
2958-
Self::Closure => "closure",
2959-
Self::Opaque => "opaque type",
2960-
Self::OpaqueFuture => "future",
2961-
Self::Coroutine(gk) => gk.descr(),
2962-
Self::Foreign => "foreign type",
2957+
Self::Closure => "closure".fmt(f),
2958+
Self::Opaque => "opaque type".fmt(f),
2959+
Self::OpaqueFuture => "future".fmt(f),
2960+
Self::Coroutine(gk) => gk.fmt(f),
2961+
Self::Foreign => "foreign type".fmt(f),
29632962
}
29642963
}
2964+
}
29652965

2966+
impl TyCategory {
29662967
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
29672968
match *ty.kind() {
29682969
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),

0 commit comments

Comments
 (0)