Skip to content

Commit c1345e1

Browse files
Auto merge of #150843 - fmease:dyn-ace, r=<try>
mGCA: Make trait object types with type associated consts dyn compatible if the latter are specified via bindings
2 parents 4586feb + c11d680 commit c1345e1

140 files changed

Lines changed: 1032 additions & 566 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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ dependencies = [
308308
"libc",
309309
"miniz_oxide",
310310
"object 0.36.7",
311-
"rustc-demangle",
311+
"rustc-demangle 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
312312
"windows-targets 0.52.6",
313313
]
314314

@@ -937,7 +937,7 @@ dependencies = [
937937
"md-5",
938938
"miniz_oxide",
939939
"regex",
940-
"rustc-demangle",
940+
"rustc-demangle 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
941941
]
942942

943943
[[package]]
@@ -3398,6 +3398,11 @@ version = "0.1.26"
33983398
source = "registry+https://github.com/rust-lang/crates.io-index"
33993399
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
34003400

3401+
[[package]]
3402+
name = "rustc-demangle"
3403+
version = "0.1.26"
3404+
source = "git+https://github.com/fmease/rustc-demangle.git?rev=2631bdf63859c05ad2dba93726a43ed9a7427c9c#2631bdf63859c05ad2dba93726a43ed9a7427c9c"
3405+
34013406
[[package]]
34023407
name = "rustc-hash"
34033408
version = "1.1.0"
@@ -3668,7 +3673,7 @@ dependencies = [
36683673
"libloading 0.9.0",
36693674
"measureme",
36703675
"object 0.37.3",
3671-
"rustc-demangle",
3676+
"rustc-demangle 0.1.26 (git+https://github.com/fmease/rustc-demangle.git?rev=2631bdf63859c05ad2dba93726a43ed9a7427c9c)",
36723677
"rustc_abi",
36733678
"rustc_ast",
36743679
"rustc_codegen_ssa",
@@ -4731,7 +4736,7 @@ name = "rustc_symbol_mangling"
47314736
version = "0.0.0"
47324737
dependencies = [
47334738
"punycode",
4734-
"rustc-demangle",
4739+
"rustc-demangle 0.1.26 (git+https://github.com/fmease/rustc-demangle.git?rev=2631bdf63859c05ad2dba93726a43ed9a7427c9c)",
47354740
"rustc_abi",
47364741
"rustc_data_structures",
47374742
"rustc_errors",

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,7 @@ impl FnSig {
23952395
/// * the `G<Ty> = Ty` in `Trait<G<Ty> = Ty>`
23962396
/// * the `A: Bound` in `Trait<A: Bound>`
23972397
/// * the `RetTy` in `Trait(ArgTy, ArgTy) -> RetTy`
2398-
/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `associated_const_equality`)
2398+
/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `min_generic_const_args`)
23992399
/// * the `f(..): Bound` in `Trait<f(..): Bound>` (feature `return_type_notation`)
24002400
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
24012401
pub struct AssocItemConstraint {

compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ libc = "0.2"
1717
libloading = { version = "0.9.0" }
1818
measureme = "12.0.1"
1919
object = { version = "0.37.0", default-features = false, features = ["std", "read"] }
20-
rustc-demangle = "0.1.21"
20+
# FIXME(fmease): Temporary until <https://github.com/rust-lang/rustc-demangle/pull/87> gets merged.
21+
# rustc-demangle = "0.1.21"
22+
rustc-demangle = { git = "https://github.com/fmease/rustc-demangle.git", rev = "2631bdf63859c05ad2dba93726a43ed9a7427c9c" }
2123
rustc_abi = { path = "../rustc_abi" }
2224
rustc_ast = { path = "../rustc_ast" }
2325
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ pub enum ImplItemKind<'hir> {
32903290
/// * the `G<Ty> = Ty` in `Trait<G<Ty> = Ty>`
32913291
/// * the `A: Bound` in `Trait<A: Bound>`
32923292
/// * the `RetTy` in `Trait(ArgTy, ArgTy) -> RetTy`
3293-
/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `associated_const_equality`)
3293+
/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `min_generic_const_args`)
32943294
/// * the `f(..): Bound` in `Trait<f(..): Bound>` (feature `return_type_notation`)
32953295
#[derive(Debug, Clone, Copy, HashStable_Generic)]
32963296
pub struct AssocItemConstraint<'hir> {

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -329,37 +329,18 @@ hir_analysis_manual_implementation =
329329
hir_analysis_method_should_return_future = method should be `async` or return a future, but it is synchronous
330330
.note = this method is `async` so it expects a future to be returned
331331
332-
hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
333-
.label = missing one of `{$missing_items_msg}` in implementation
334-
.note = required because of this annotation
335-
336-
hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
337-
.label = missing `{$missing_items_msg}` in implementation
338-
339-
hir_analysis_missing_trait_item_label = `{$item}` from trait
340-
341-
hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`
342-
343-
hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
344-
.note = default implementation of `{$missing_item_name}` is unstable
345-
.some_note = use of unstable library feature `{$feature}`: {$reason}
346-
.none_note = use of unstable library feature `{$feature}`
347-
348-
hir_analysis_missing_type_params =
349-
the type {$parameterCount ->
332+
hir_analysis_missing_generic_params =
333+
the {$descr} {$parameterCount ->
350334
[one] parameter
351335
*[other] parameters
352336
} {$parameters} must be explicitly specified
353-
.label = type {$parameterCount ->
337+
.label = {$descr} {$parameterCount ->
354338
[one] parameter
355339
*[other] parameters
356340
} {$parameters} must be specified for this
357-
.suggestion = set the type {$parameterCount ->
341+
.suggestion = explicitly specify the {$descr} {$parameterCount ->
358342
[one] parameter
359343
*[other] parameters
360-
} to the desired {$parameterCount ->
361-
[one] type
362-
*[other] types
363344
}
364345
.no_suggestion_label = missing {$parameterCount ->
365346
[one] reference
@@ -371,7 +352,23 @@ hir_analysis_missing_type_params =
371352
} `Self`, the {$parameterCount ->
372353
[one] parameter
373354
*[other] parameters
374-
} must be specified on the object type
355+
} must be specified on the trait object type
356+
357+
hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
358+
.label = missing one of `{$missing_items_msg}` in implementation
359+
.note = required because of this annotation
360+
361+
hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
362+
.label = missing `{$missing_items_msg}` in implementation
363+
364+
hir_analysis_missing_trait_item_label = `{$item}` from trait
365+
366+
hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`
367+
368+
hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
369+
.note = default implementation of `{$missing_item_name}` is unstable
370+
.some_note = use of unstable library feature `{$feature}`: {$reason}
371+
.none_note = use of unstable library feature `{$feature}`
375372
376373
hir_analysis_no_variant_named = no variant named `{$ident}` found for enum `{$ty}`
377374

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/collect/type_of.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use core::ops::ControlFlow;
22

33
use rustc_errors::{Applicability, StashKey, Suggestions};
4+
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::def::DefKind;
46
use rustc_hir::def_id::{DefId, LocalDefId};
57
use rustc_hir::intravisit::VisitorExt;
6-
use rustc_hir::{self as hir, AmbigArg, HirId};
8+
use rustc_hir::{self as hir, AmbigArg, HirId, find_attr};
79
use rustc_middle::query::plumbing::CyclePlaceholder;
810
use rustc_middle::ty::print::with_forced_trimmed_paths;
911
use rustc_middle::ty::util::IntTypeExt;
@@ -420,7 +422,28 @@ fn infer_placeholder_type<'tcx>(
420422
kind: &'static str,
421423
) -> Ty<'tcx> {
422424
let tcx = cx.tcx();
423-
let ty = tcx.typeck(def_id).node_type(hir_id);
425+
426+
fn is_type_const<'tcx>(tcx: TyCtxt<'tcx>, id: DefId) -> bool {
427+
matches!(tcx.def_kind(id), DefKind::Const | DefKind::AssocConst)
428+
&& find_attr!(tcx.get_all_attrs(id), AttributeKind::TypeConst(_))
429+
}
430+
431+
// If the type is omitted on a [type_const] we can't run
432+
// type check on since that requires the const have a body
433+
// which type_consts don't.
434+
let ty = if is_type_const(tcx, def_id.to_def_id()) {
435+
if let Some(trait_item_def_id) = tcx.trait_item_of(def_id.to_def_id()) {
436+
tcx.type_of(trait_item_def_id).instantiate_identity()
437+
} else {
438+
Ty::new_error_with_message(
439+
tcx,
440+
ty_span,
441+
"associated constant with [type_const] requires an explicit type",
442+
)
443+
}
444+
} else {
445+
tcx.typeck(def_id).node_type(hir_id)
446+
};
424447

425448
// If this came from a free `const` or `static mut?` item,
426449
// then the user may have written e.g. `const A = 42;`.

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 43 additions & 20 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
),

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
10231023
.collect::<Vec<_>>();
10241024

10251025
if !suggestions.is_empty() {
1026+
// FIXME: This suggests binding to an assoc ty even if the term is a const.
1027+
// FIXME: Generalize this to cover assoc consts, too.
10261028
err.multipart_suggestion_verbose(
10271029
format!(
10281030
"replace the generic bound{s} with the associated type{s}",

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
575575
// FIXME: point at the type params that don't have appropriate lifetimes:
576576
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
577577
// ---- ---- ^^^^^^^
578-
// NOTE(associated_const_equality): This error should be impossible to trigger
579-
// with associated const equality constraints.
578+
// NOTE(mgca): This error should be impossible to trigger with assoc const bindings.
580579
self.validate_late_bound_regions(
581580
late_bound_in_projection_ty,
582581
late_bound_in_term,

0 commit comments

Comments
 (0)