Skip to content

Commit 2641353

Browse files
committed
Auto merge of #150843 - fmease:dyn-ace, r=BoxyUwU
mGCA: Make trait object types with type-level associated consts dyn compatible if all of the latter are specified via bindings Under feature `min_generic_const_args` (mGCA) (#132980), render traits with non-parametrized type-level associated constants (i.e., `#[type_const]` ones) dyn compatible but force the user to specify all type-level associated consts in the trait object type via bindings (either directly, via supertrait bounds and/or behind trait aliases) just like associated types, their sibling. Fixes #130300 (feature request). Fixes #136063 (bug). Fixes #137260 (bug). Fixes #137514 (bug). While I'm accounting for most illegal `Self` references via const projections & params, I'm intentionally ignoring RUST-123140 (and duplicates) in this PR which is to be tackled some other time. Additional context: Crate `rustc-demangle` had to be updated to fix v0 demangling. I've patched it in PR rust-lang/rustc-demangle#87 which was was released in version 0.1.27 via PR rust-lang/rustc-demangle#88.
2 parents fffc4fc + 87fe6b1 commit 2641353

104 files changed

Lines changed: 1360 additions & 656 deletions

File tree

Some content is hidden

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

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,9 +3355,9 @@ dependencies = [
33553355

33563356
[[package]]
33573357
name = "rustc-demangle"
3358-
version = "0.1.26"
3358+
version = "0.1.27"
33593359
source = "registry+https://github.com/rust-lang/crates.io-index"
3360-
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
3360+
checksum = "b50b8869d9fc858ce7266cce0194bd74df58b9d0e3f6df3a9fc8eb470d95c09d"
33613361

33623362
[[package]]
33633363
name = "rustc-hash"

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ hir_analysis_drop_impl_reservation = reservation `Drop` impls are not supported
165165
hir_analysis_duplicate_precise_capture = cannot capture parameter `{$name}` twice
166166
.label = parameter captured again here
167167
168+
hir_analysis_dyn_trait_assoc_item_binding_mentions_self =
169+
{$kind} binding in trait object type mentions `Self`
170+
.label = contains a mention of `Self`
171+
.binding_label = this binding mentions `Self`
172+
168173
hir_analysis_eii_with_generics =
169174
`{$impl_name}` cannot have generic parameters other than lifetimes
170175
.label = required by this attribute
@@ -330,37 +335,18 @@ hir_analysis_manual_implementation =
330335
hir_analysis_method_should_return_future = method should be `async` or return a future, but it is synchronous
331336
.note = this method is `async` so it expects a future to be returned
332337
333-
hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
334-
.label = missing one of `{$missing_items_msg}` in implementation
335-
.note = required because of this annotation
336-
337-
hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
338-
.label = missing `{$missing_items_msg}` in implementation
339-
340-
hir_analysis_missing_trait_item_label = `{$item}` from trait
341-
342-
hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`
343-
344-
hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
345-
.note = default implementation of `{$missing_item_name}` is unstable
346-
.some_note = use of unstable library feature `{$feature}`: {$reason}
347-
.none_note = use of unstable library feature `{$feature}`
348-
349-
hir_analysis_missing_type_params =
350-
the type {$parameterCount ->
338+
hir_analysis_missing_generic_params =
339+
the {$descr} {$parameterCount ->
351340
[one] parameter
352341
*[other] parameters
353342
} {$parameters} must be explicitly specified
354-
.label = type {$parameterCount ->
343+
.label = {$descr} {$parameterCount ->
355344
[one] parameter
356345
*[other] parameters
357346
} {$parameters} must be specified for this
358-
.suggestion = set the type {$parameterCount ->
347+
.suggestion = explicitly specify the {$descr} {$parameterCount ->
359348
[one] parameter
360349
*[other] parameters
361-
} to the desired {$parameterCount ->
362-
[one] type
363-
*[other] types
364350
}
365351
.no_suggestion_label = missing {$parameterCount ->
366352
[one] reference
@@ -372,7 +358,23 @@ hir_analysis_missing_type_params =
372358
} `Self`, the {$parameterCount ->
373359
[one] parameter
374360
*[other] parameters
375-
} must be specified on the object type
361+
} must be specified on the trait object type
362+
363+
hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
364+
.label = missing one of `{$missing_items_msg}` in implementation
365+
.note = required because of this annotation
366+
367+
hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
368+
.label = missing `{$missing_items_msg}` in implementation
369+
370+
hir_analysis_missing_trait_item_label = `{$item}` from trait
371+
372+
hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`
373+
374+
hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
375+
.note = default implementation of `{$missing_item_name}` is unstable
376+
.some_note = use of unstable library feature `{$feature}`: {$reason}
377+
.none_note = use of unstable library feature `{$feature}`
376378
377379
hir_analysis_no_variant_named = no variant named `{$ident}` found for enum `{$ty}`
378380
@@ -481,9 +483,6 @@ hir_analysis_self_in_impl_self =
481483
`Self` is not valid in the self type of an impl block
482484
.note = replace `Self` with a different type
483485
484-
hir_analysis_self_in_type_alias = `Self` is not allowed in type aliases
485-
.label = `Self` is only available in impls, traits, and concrete type definitions
486-
487486
hir_analysis_self_ty_not_captured = `impl Trait` must mention the `Self` type of the trait in `use<...>`
488487
.label = `Self` type parameter is implicitly captured by this `impl Trait`
489488
.note = currently, all type parameters are required to be mentioned in the precise captures list

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,7 @@ fn compare_generic_param_kinds<'tcx>(
19601960
trait_item: ty::AssocItem,
19611961
delay: bool,
19621962
) -> Result<(), ErrorGuaranteed> {
1963-
assert_eq!(impl_item.as_tag(), trait_item.as_tag());
1963+
assert_eq!(impl_item.tag(), trait_item.tag());
19641964

19651965
let ty_const_params_of = |def_id| {
19661966
tcx.generics_of(def_id).own_params.iter().filter(|param| {

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use rustc_abi::ExternAbi;
44
use rustc_errors::codes::*;
55
use rustc_errors::{
66
Applicability, Diag, DiagCtxtHandle, DiagSymbolList, Diagnostic, EmissionGuarantee, Level,
7-
MultiSpan,
7+
MultiSpan, listify,
88
};
99
use rustc_hir::limit::Limit;
1010
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
11-
use rustc_middle::ty::Ty;
11+
use rustc_middle::ty::{self, Ty};
1212
use rustc_span::{Ident, Span, Symbol};
1313

1414
use crate::fluent_generated as fluent;
@@ -400,35 +400,58 @@ pub(crate) struct UnconstrainedOpaqueType {
400400
pub what: &'static str,
401401
}
402402

403-
pub(crate) struct MissingTypeParams {
403+
pub(crate) struct MissingGenericParams {
404404
pub span: Span,
405405
pub def_span: Span,
406406
pub span_snippet: Option<String>,
407-
pub missing_type_params: Vec<Symbol>,
407+
pub missing_generic_params: Vec<(Symbol, ty::GenericParamDefKind)>,
408408
pub empty_generic_args: bool,
409409
}
410410

411-
// Manual implementation of `Diagnostic` to be able to call `span_to_snippet`.
412-
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
411+
// FIXME: This doesn't need to be a manual impl!
412+
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingGenericParams {
413413
#[track_caller]
414414
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
415-
let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_type_params);
415+
let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_generic_params);
416416
err.span(self.span);
417417
err.code(E0393);
418-
err.arg("parameterCount", self.missing_type_params.len());
418+
err.span_label(self.def_span, fluent::hir_analysis_label);
419+
420+
enum Descr {
421+
Generic,
422+
Type,
423+
Const,
424+
}
425+
426+
let mut descr = None;
427+
for (_, kind) in &self.missing_generic_params {
428+
descr = match (&descr, kind) {
429+
(None, ty::GenericParamDefKind::Type { .. }) => Some(Descr::Type),
430+
(None, ty::GenericParamDefKind::Const { .. }) => Some(Descr::Const),
431+
(Some(Descr::Type), ty::GenericParamDefKind::Const { .. })
432+
| (Some(Descr::Const), ty::GenericParamDefKind::Type { .. }) => {
433+
Some(Descr::Generic)
434+
}
435+
_ => continue,
436+
}
437+
}
438+
439+
err.arg(
440+
"descr",
441+
match descr.unwrap() {
442+
Descr::Generic => "generic",
443+
Descr::Type => "type",
444+
Descr::Const => "const",
445+
},
446+
);
447+
err.arg("parameterCount", self.missing_generic_params.len());
419448
err.arg(
420449
"parameters",
421-
self.missing_type_params
422-
.iter()
423-
.map(|n| format!("`{n}`"))
424-
.collect::<Vec<_>>()
425-
.join(", "),
450+
listify(&self.missing_generic_params, |(n, _)| format!("`{n}`")).unwrap(),
426451
);
427452

428-
err.span_label(self.def_span, fluent::hir_analysis_label);
429-
430453
let mut suggested = false;
431-
// Don't suggest setting the type params if there are some already: the order is
454+
// Don't suggest setting the generic params if there are some already: The order is
432455
// tricky to get right and the user will already know what the syntax is.
433456
if let Some(snippet) = self.span_snippet
434457
&& self.empty_generic_args
@@ -438,16 +461,16 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
438461
// we would have to preserve the right order. For now, as clearly the user is
439462
// aware of the syntax, we do nothing.
440463
} else {
441-
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
442-
// least we can clue them to the correct syntax `Iterator<Type>`.
464+
// The user wrote `Trait`, so we don't have a type we can suggest, but at
465+
// least we can clue them to the correct syntax `Trait</* Term */>`.
443466
err.span_suggestion_verbose(
444467
self.span.shrink_to_hi(),
445468
fluent::hir_analysis_suggestion,
446469
format!(
447470
"<{}>",
448-
self.missing_type_params
471+
self.missing_generic_params
449472
.iter()
450-
.map(|n| n.to_string())
473+
.map(|(n, _)| format!("/* {n} */"))
451474
.collect::<Vec<_>>()
452475
.join(", ")
453476
),
@@ -1609,11 +1632,14 @@ pub(crate) enum SupertraitItemShadowee {
16091632
}
16101633

16111634
#[derive(Diagnostic)]
1612-
#[diag(hir_analysis_self_in_type_alias, code = E0411)]
1613-
pub(crate) struct SelfInTypeAlias {
1635+
#[diag(hir_analysis_dyn_trait_assoc_item_binding_mentions_self)]
1636+
pub(crate) struct DynTraitAssocItemBindingMentionsSelf {
16141637
#[primary_span]
16151638
#[label]
16161639
pub span: Span,
1640+
pub kind: &'static str,
1641+
#[label(hir_analysis_binding_label)]
1642+
pub binding: Span,
16171643
}
16181644

16191645
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)