Skip to content
Draft
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
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3352,9 +3352,9 @@ dependencies = [

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

[[package]]
name = "rustc-hash"
Expand Down
53 changes: 26 additions & 27 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ hir_analysis_drop_impl_reservation = reservation `Drop` impls are not supported
hir_analysis_duplicate_precise_capture = cannot capture parameter `{$name}` twice
.label = parameter captured again here

hir_analysis_dyn_trait_assoc_item_binding_mentions_self =
{$kind} binding in trait object type mentions `Self`
.label = contains a mention of `Self`
.binding_label = this binding mentions `Self`

hir_analysis_eii_with_generics =
#[{$eii_name}] cannot have generic parameters other than lifetimes
.label = required by this attribute
Expand Down Expand Up @@ -329,37 +334,18 @@ hir_analysis_manual_implementation =
hir_analysis_method_should_return_future = method should be `async` or return a future, but it is synchronous
.note = this method is `async` so it expects a future to be returned

hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
.label = missing one of `{$missing_items_msg}` in implementation
.note = required because of this annotation

hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
.label = missing `{$missing_items_msg}` in implementation

hir_analysis_missing_trait_item_label = `{$item}` from trait

hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`

hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
.note = default implementation of `{$missing_item_name}` is unstable
.some_note = use of unstable library feature `{$feature}`: {$reason}
.none_note = use of unstable library feature `{$feature}`

hir_analysis_missing_type_params =
the type {$parameterCount ->
hir_analysis_missing_generic_params =
the {$descr} {$parameterCount ->
[one] parameter
*[other] parameters
} {$parameters} must be explicitly specified
.label = type {$parameterCount ->
.label = {$descr} {$parameterCount ->
[one] parameter
*[other] parameters
} {$parameters} must be specified for this
.suggestion = set the type {$parameterCount ->
.suggestion = explicitly specify the {$descr} {$parameterCount ->
[one] parameter
*[other] parameters
} to the desired {$parameterCount ->
[one] type
*[other] types
}
.no_suggestion_label = missing {$parameterCount ->
[one] reference
Expand All @@ -371,7 +357,23 @@ hir_analysis_missing_type_params =
} `Self`, the {$parameterCount ->
[one] parameter
*[other] parameters
} must be specified on the object type
} must be specified on the trait object type

hir_analysis_missing_one_of_trait_item = not all trait items implemented, missing one of: `{$missing_items_msg}`
.label = missing one of `{$missing_items_msg}` in implementation
.note = required because of this annotation

hir_analysis_missing_trait_item = not all trait items implemented, missing: `{$missing_items_msg}`
.label = missing `{$missing_items_msg}` in implementation

hir_analysis_missing_trait_item_label = `{$item}` from trait

hir_analysis_missing_trait_item_suggestion = implement the missing item: `{$snippet}`

hir_analysis_missing_trait_item_unstable = not all trait items implemented, missing: `{$missing_item_name}`
.note = default implementation of `{$missing_item_name}` is unstable
.some_note = use of unstable library feature `{$feature}`: {$reason}
.none_note = use of unstable library feature `{$feature}`

hir_analysis_no_variant_named = no variant named `{$ident}` found for enum `{$ty}`

Expand Down Expand Up @@ -480,9 +482,6 @@ hir_analysis_self_in_impl_self =
`Self` is not valid in the self type of an impl block
.note = replace `Self` with a different type

hir_analysis_self_in_type_alias = `Self` is not allowed in type aliases
.label = `Self` is only available in impls, traits, and concrete type definitions

hir_analysis_self_ty_not_captured = `impl Trait` must mention the `Self` type of the trait in `use<...>`
.label = `Self` type parameter is implicitly captured by this `impl Trait`
.note = currently, all type parameters are required to be mentioned in the precise captures list
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,7 @@ fn compare_generic_param_kinds<'tcx>(
trait_item: ty::AssocItem,
delay: bool,
) -> Result<(), ErrorGuaranteed> {
assert_eq!(impl_item.as_tag(), trait_item.as_tag());
assert_eq!(impl_item.tag(), trait_item.tag());

let ty_const_params_of = |def_id| {
tcx.generics_of(def_id).own_params.iter().filter(|param| {
Expand Down
70 changes: 48 additions & 22 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rustc_abi::ExternAbi;
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, DiagSymbolList, Diagnostic, EmissionGuarantee, Level,
MultiSpan,
MultiSpan, listify,
};
use rustc_hir::limit::Limit;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::Ty;
use rustc_middle::ty::{self, Ty};
use rustc_span::{Ident, Span, Symbol};

use crate::fluent_generated as fluent;
Expand Down Expand Up @@ -400,35 +400,58 @@ pub(crate) struct UnconstrainedOpaqueType {
pub what: &'static str,
}

pub(crate) struct MissingTypeParams {
pub(crate) struct MissingGenericParams {
pub span: Span,
pub def_span: Span,
pub span_snippet: Option<String>,
pub missing_type_params: Vec<Symbol>,
pub missing_generic_params: Vec<(Symbol, ty::GenericParamDefKind)>,
pub empty_generic_args: bool,
}

// Manual implementation of `Diagnostic` to be able to call `span_to_snippet`.
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
// FIXME: This doesn't need to be a manual impl!
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingGenericParams {
#[track_caller]
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_type_params);
let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_generic_params);
err.span(self.span);
err.code(E0393);
err.arg("parameterCount", self.missing_type_params.len());
err.span_label(self.def_span, fluent::hir_analysis_label);

enum Descr {
Generic,
Type,
Const,
}

let mut descr = None;
for (_, kind) in &self.missing_generic_params {
descr = match (&descr, kind) {
(None, ty::GenericParamDefKind::Type { .. }) => Some(Descr::Type),
(None, ty::GenericParamDefKind::Const { .. }) => Some(Descr::Const),
(Some(Descr::Type), ty::GenericParamDefKind::Const { .. })
| (Some(Descr::Const), ty::GenericParamDefKind::Type { .. }) => {
Some(Descr::Generic)
}
_ => continue,
}
}

err.arg(
"descr",
match descr.unwrap() {
Descr::Generic => "generic",
Descr::Type => "type",
Descr::Const => "const",
},
);
err.arg("parameterCount", self.missing_generic_params.len());
err.arg(
"parameters",
self.missing_type_params
.iter()
.map(|n| format!("`{n}`"))
.collect::<Vec<_>>()
.join(", "),
listify(&self.missing_generic_params, |(n, _)| format!("`{n}`")).unwrap(),
);

err.span_label(self.def_span, fluent::hir_analysis_label);

let mut suggested = false;
// Don't suggest setting the type params if there are some already: the order is
// Don't suggest setting the generic params if there are some already: The order is
// tricky to get right and the user will already know what the syntax is.
if let Some(snippet) = self.span_snippet
&& self.empty_generic_args
Expand All @@ -438,16 +461,16 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
// we would have to preserve the right order. For now, as clearly the user is
// aware of the syntax, we do nothing.
} else {
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
// least we can clue them to the correct syntax `Iterator<Type>`.
// The user wrote `Trait`, so we don't have a type we can suggest, but at
// least we can clue them to the correct syntax `Trait</* Term */>`.
err.span_suggestion_verbose(
self.span.shrink_to_hi(),
fluent::hir_analysis_suggestion,
format!(
"<{}>",
self.missing_type_params
self.missing_generic_params
.iter()
.map(|n| n.to_string())
.map(|(n, _)| format!("/* {n} */"))
.collect::<Vec<_>>()
.join(", ")
),
Expand Down Expand Up @@ -1609,11 +1632,14 @@ pub(crate) enum SupertraitItemShadowee {
}

#[derive(Diagnostic)]
#[diag(hir_analysis_self_in_type_alias, code = E0411)]
pub(crate) struct SelfInTypeAlias {
#[diag(hir_analysis_dyn_trait_assoc_item_binding_mentions_self)]
pub(crate) struct DynTraitAssocItemBindingMentionsSelf {
#[primary_span]
#[label]
pub span: Span,
pub kind: &'static str,
#[label(hir_analysis_binding_label)]
pub binding: Span,
}

#[derive(Diagnostic)]
Expand Down
Loading
Loading