Skip to content

Rollup of 7 pull requests #115754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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: 4 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ Gareth Daniel Smith <garethdanielsmith@gmail.com> gareth <gareth@gareth-N56VM.(n
Gareth Daniel Smith <garethdanielsmith@gmail.com> Gareth Smith <garethdanielsmith@gmail.com>
Gauri Kholkar <f2013002@goa.bits-pilani.ac.in>
Georges Dubus <georges.dubus@gmail.com> <georges.dubus@compiletoi.net>
Ghost <ghost> <jonasschievink@gmail.com>
Ghost <ghost> <jonas.schievink@ferrous-systems.com>
Ghost <ghost> <jonas@schievink.net>
Ghost <ghost> <Jonas.Schievink@sony.com>
Giles Cope <gilescope@gmail.com>
Glen De Cauwsemaecker <decauwsemaecker.glen@gmail.com>
Graham Fawcett <graham.fawcett@gmail.com> Graham Fawcett <fawcett@uwindsor.ca>
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0401.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Inner items do not inherit type or const parameters from the functions
Inner items do not inherit the generic parameters from the items
they are embedded in.

Erroneous code example:
Expand Down Expand Up @@ -32,8 +32,8 @@ fn foo<T>(x: T) {
}
```

Items inside functions are basically just like top-level items, except
that they can only be used from the function they are in.
Items nested inside other items are basically just like top-level items, except
that they can only be used from the item they are in.

There are a couple of solutions for this.

Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_hir_typeck/src/generator_interior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,14 @@ fn check_must_not_suspend_ty<'tcx>(
}
ty::Array(ty, len) => {
let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
let target_usize =
len.try_eval_target_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize;
let plural_len = target_usize.saturating_add(1);
check_must_not_suspend_ty(
fcx,
ty,
hir_id,
SuspendCheckData {
descr_pre,
plural_len: len.try_eval_target_usize(fcx.tcx, fcx.param_env).unwrap_or(0)
as usize
+ 1,
..data
},
SuspendCheckData { descr_pre, plural_len, ..data },
)
}
// If drop tracking is enabled, we want to look through references, since the referent
Expand Down
25 changes: 12 additions & 13 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use rustc_data_structures::sync::join;
use rustc_hir as hir;
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
use rustc_hir::intravisit as hir_visit;
use rustc_hir::intravisit::Visitor;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::LintPass;
Expand Down Expand Up @@ -61,6 +60,9 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
self.context.last_node_with_lint_attrs = id;
debug!("late context: enter_attrs({:?})", attrs);
lint_callback!(self, enter_lint_attrs, attrs);
for attr in attrs {
lint_callback!(self, check_attribute, attr);
}
f(self);
debug!("late context: exit_attrs({:?})", attrs);
lint_callback!(self, exit_lint_attrs, attrs);
Expand Down Expand Up @@ -377,20 +379,18 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(

let (module, _span, hir_id) = tcx.hir().get_module(module_def_id);

// There is no module lint that will have the crate itself as an item, so check it here.
if hir_id == hir::CRATE_HIR_ID {
lint_callback!(cx, check_crate,);
}
cx.with_lint_attrs(hir_id, |cx| {
// There is no module lint that will have the crate itself as an item, so check it here.
if hir_id == hir::CRATE_HIR_ID {
lint_callback!(cx, check_crate,);
}

cx.process_mod(module, hir_id);
cx.process_mod(module, hir_id);

// Visit the crate attributes
if hir_id == hir::CRATE_HIR_ID {
for attr in tcx.hir().attrs(hir::CRATE_HIR_ID).iter() {
cx.visit_attribute(attr)
if hir_id == hir::CRATE_HIR_ID {
lint_callback!(cx, check_crate_post,);
}
lint_callback!(cx, check_crate_post,);
}
});
}

fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
Expand Down Expand Up @@ -431,7 +431,6 @@ fn late_lint_crate_inner<'tcx, T: LateLintPass<'tcx>>(
// item), warn for it here.
lint_callback!(cx, check_crate,);
tcx.hir().walk_toplevel_module(cx);
tcx.hir().walk_attributes(cx);
lint_callback!(cx, check_crate_post,);
})
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ pub struct TraitImpls {
}

impl TraitImpls {
pub fn is_empty(&self) -> bool {
self.blanket_impls.is_empty() && self.non_blanket_impls.is_empty()
}

pub fn blanket_impls(&self) -> &[DefId] {
self.blanket_impls.as_slice()
}
Expand Down
29 changes: 13 additions & 16 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ resolve_cannot_find_ident_in_this_scope =
resolve_cannot_glob_import_possible_crates =
cannot glob-import all possible crates

resolve_cannot_use_self_type_here =
can't use `Self` here

resolve_change_import_binding =
you can use `as` to change the binding name of the import

Expand All @@ -90,9 +87,6 @@ resolve_const_not_member_of_trait =
const `{$const_}` is not a member of trait `{$trait_}`
.label = not a member of trait `{$trait_}`

resolve_const_param_from_outer_fn =
const parameter from outer function

resolve_const_param_in_enum_discriminant =
const parameters may not be used in enum discriminant values

Expand All @@ -119,10 +113,19 @@ resolve_forward_declared_generic_param =
generic parameters with a default cannot use forward declared identifiers
.label = defaulted generic parameters cannot be forward declared

resolve_generic_params_from_outer_function =
can't use generic parameters from outer function
.label = use of generic parameter from outer function
.suggestion = try using a local generic parameter instead
resolve_generic_params_from_outer_item =
can't use generic parameters from outer item
.label = use of generic parameter from outer item
.refer_to_type_directly = refer to the type directly here instead
.suggestion = try introducing a local generic parameter here

resolve_generic_params_from_outer_item_const_param = const parameter from outer item

resolve_generic_params_from_outer_item_self_ty_alias = `Self` type implicitly declared here, by this `impl`

resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here

resolve_generic_params_from_outer_item_ty_param = type parameter from outer item

resolve_glob_import_doesnt_reexport =
glob import doesn't reexport anything because no candidate is public enough
Expand Down Expand Up @@ -277,9 +280,6 @@ resolve_type_not_member_of_trait =
type `{$type_}` is not a member of trait `{$trait_}`
.label = not a member of trait `{$trait_}`

resolve_type_param_from_outer_fn =
type parameter from outer function

resolve_type_param_in_enum_discriminant =
type parameters may not be used in enum discriminant values

Expand Down Expand Up @@ -315,9 +315,6 @@ resolve_unreachable_label_suggestion_use_similarly_named =
resolve_unreachable_label_with_similar_name_exists =
a label with a similar name exists but is unreachable

resolve_use_a_type_here_instead =
use a type here instead

resolve_variable_bound_with_different_mode =
variable `{$variable_name}` is bound inconsistently across alternatives separated by `|`
.label = bound in different ways
Expand Down
47 changes: 20 additions & 27 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,53 +553,47 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
resolution_error: ResolutionError<'a>,
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
match resolution_error {
ResolutionError::GenericParamsFromOuterFunction(outer_res, has_generic_params) => {
let mut err = struct_span_err!(
self.tcx.sess,
ResolutionError::GenericParamsFromOuterItem(outer_res, has_generic_params) => {
use errs::GenericParamsFromOuterItemLabel as Label;
let mut err = errs::GenericParamsFromOuterItem {
span,
E0401,
"can't use generic parameters from outer function",
);
err.span_label(span, "use of generic parameter from outer function");
label: None,
refer_to_type_directly: None,
sugg: None,
};

let sm = self.tcx.sess.source_map();
let def_id = match outer_res {
Res::SelfTyParam { .. } => {
err.span_label(span, "can't use `Self` here");
return err;
err.label = Some(Label::SelfTyParam(span));
return self.tcx.sess.create_err(err);
}
Res::SelfTyAlias { alias_to: def_id, .. } => {
err.span_label(
reduce_impl_span_to_impl_keyword(sm, self.def_span(def_id)),
"`Self` type implicitly declared here, by this `impl`",
);
err.span_label(span, "use a type here instead");
return err;
err.label = Some(Label::SelfTyAlias(reduce_impl_span_to_impl_keyword(
sm,
self.def_span(def_id),
)));
err.refer_to_type_directly = Some(span);
return self.tcx.sess.create_err(err);
}
Res::Def(DefKind::TyParam, def_id) => {
err.span_label(self.def_span(def_id), "type parameter from outer function");
err.label = Some(Label::TyParam(self.def_span(def_id)));
def_id
}
Res::Def(DefKind::ConstParam, def_id) => {
err.span_label(
self.def_span(def_id),
"const parameter from outer function",
);
err.label = Some(Label::ConstParam(self.def_span(def_id)));
def_id
}
_ => {
bug!(
"GenericParamsFromOuterFunction should only be used with \
"GenericParamsFromOuterItem should only be used with \
Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
DefKind::ConstParam"
);
}
};

if let HasGenericParams::Yes(span) = has_generic_params {
// Try to retrieve the span of the function signature and generate a new
// message with a local type or const parameter.
let sugg_msg = "try using a local generic parameter instead";
let name = self.tcx.item_name(def_id);
let (span, snippet) = if span.is_empty() {
let snippet = format!("<{name}>");
Expand All @@ -609,11 +603,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let snippet = format!("{name}, ");
(span, snippet)
};
// Suggest the modification to the user
err.span_suggestion(span, sugg_msg, snippet, Applicability::MaybeIncorrect);
err.sugg = Some(errs::GenericParamsFromOuterItemSugg { span, snippet });
}

err
self.tcx.sess.create_err(err)
}
ResolutionError::NameAlreadyUsedInParameterList(name, first_use_span) => self
.tcx
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ pub(crate) struct CrateRootNamesMustBeNamedExplicitly(#[primary_span] pub(crate)
#[diag(resolve_crate_root_imports_must_be_named_explicitly)]
pub(crate) struct ResolutionError(#[primary_span] pub(crate) Span);

#[derive(Diagnostic)]
#[diag(resolve_generic_params_from_outer_item, code = "E0401")]
pub(crate) struct GenericParamsFromOuterItem {
#[primary_span]
#[label]
pub(crate) span: Span,
#[subdiagnostic]
pub(crate) label: Option<GenericParamsFromOuterItemLabel>,
#[label(resolve_refer_to_type_directly)]
pub(crate) refer_to_type_directly: Option<Span>,
#[subdiagnostic]
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
}

#[derive(Subdiagnostic)]
pub(crate) enum GenericParamsFromOuterItemLabel {
#[label(resolve_generic_params_from_outer_item_self_ty_param)]
SelfTyParam(#[primary_span] Span),
#[label(resolve_generic_params_from_outer_item_self_ty_alias)]
SelfTyAlias(#[primary_span] Span),
#[label(resolve_generic_params_from_outer_item_ty_param)]
TyParam(#[primary_span] Span),
#[label(resolve_generic_params_from_outer_item_const_param)]
ConstParam(#[primary_span] Span),
}

#[derive(Subdiagnostic)]
#[suggestion(resolve_suggestion, code = "{snippet}", applicability = "maybe-incorrect")]
pub(crate) struct GenericParamsFromOuterItemSugg {
#[primary_span]
pub(crate) span: Span,
pub(crate) snippet: String,
}

#[derive(Diagnostic)]
#[diag(resolve_name_is_already_used_as_generic_parameter, code = "E0403")]
pub(crate) struct NameAlreadyUsedInParameterList {
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,10 +1229,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(span) = finalize {
self.report_error(
span,
ResolutionError::GenericParamsFromOuterFunction(
res,
has_generic_params,
),
ResolutionError::GenericParamsFromOuterItem(res, has_generic_params),
);
}
return Res::Err;
Expand Down Expand Up @@ -1296,10 +1293,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(span) = finalize {
self.report_error(
span,
ResolutionError::GenericParamsFromOuterFunction(
res,
has_generic_params,
),
ResolutionError::GenericParamsFromOuterItem(res, has_generic_params),
);
}
return Res::Err;
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
ItemKind::Const(box ast::ConstItem { ref generics, ref ty, ref expr, .. }) => {
self.with_generic_param_rib(
&generics.params,
RibKind::Item(HasGenericParams::Yes(generics.span)),
RibKind::Item(if self.r.tcx.features().generic_const_items {
HasGenericParams::Yes(generics.span)
} else {
HasGenericParams::No
}),
LifetimeRibKind::Generics {
binder: item.id,
kind: LifetimeBinderKind::ConstItem,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ struct BindingError {

#[derive(Debug)]
enum ResolutionError<'a> {
/// Error E0401: can't use type or const parameters from outer function.
GenericParamsFromOuterFunction(Res, HasGenericParams),
/// Error E0401: can't use type or const parameters from outer item.
GenericParamsFromOuterItem(Res, HasGenericParams),
/// Error E0403: the name is already used for a type or const parameter in this generic
/// parameter list.
NameAlreadyUsedInParameterList(Symbol, Span),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ trait_selection_no_value_in_rustc_on_unimplemented = this attribute must have a
.label = expected value here
.note = eg `#[rustc_on_unimplemented(message="foo")]`
trait_selection_trait_has_no_impls = this trait has no implementations, consider adding one
trait_selection_ty_alias_overflow = in case this is a recursive type alias, consider using a struct, enum, or union instead
trait_selection_unable_to_construct_constant_value = unable to construct a constant value for the unevaluated constant {$unevaluated}
Loading