Skip to content

Commit 331bb3f

Browse files
committed
Auto merge of rust-lang#3630 - rust-lang:rustup-2024-05-25, r=saethlin
Automatic Rustup
2 parents 008f6b3 + 1d0ad04 commit 331bb3f

File tree

72 files changed

+810
-585
lines changed

Some content is hidden

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

72 files changed

+810
-585
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,7 @@ dependencies = [
42524252
"rustc_fluent_macro",
42534253
"rustc_hir",
42544254
"rustc_index",
4255+
"rustc_infer",
42554256
"rustc_macros",
42564257
"rustc_middle",
42574258
"rustc_mir_build",

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use rustc_middle::middle::debugger_visualizer::{DebuggerVisualizerFile, Debugger
2929
use rustc_middle::middle::exported_symbols;
3030
use rustc_middle::middle::exported_symbols::SymbolExportKind;
3131
use rustc_middle::middle::lang_items;
32-
use rustc_middle::mir::BinOp;
3332
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
33+
use rustc_middle::mir::BinOp;
3434
use rustc_middle::query::Providers;
3535
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
3636
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};

compiler/rustc_const_eval/src/const_eval/error.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ where
139139
ErrorHandled::TooGeneric(span)
140140
}
141141
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar, span),
142-
err_inval!(Layout(LayoutError::ReferencesError(guar))) => ErrorHandled::Reported(
143-
ReportedErrorInfo::tainted_by_errors(guar),
144-
span,
145-
),
142+
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {
143+
ErrorHandled::Reported(ReportedErrorInfo::tainted_by_errors(guar), span)
144+
}
146145
// Report remaining errors.
147146
_ => {
148147
let (our_span, frames) = get_span_and_frames();

compiler/rustc_const_eval/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#![feature(yeet_expr)]
1515
#![feature(if_let_guard)]
1616

17+
pub mod check_consts;
1718
pub mod const_eval;
1819
mod errors;
1920
pub mod interpret;
20-
pub mod transform;
2121
pub mod util;
2222

2323
use std::sync::atomic::AtomicBool;

compiler/rustc_const_eval/src/transform/mod.rs

-2
This file was deleted.

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ pub fn check_intrinsic_type(
164164
) {
165165
let generics = tcx.generics_of(intrinsic_id);
166166
let param = |n| {
167-
if let Some(&ty::GenericParamDef {
168-
name, kind: ty::GenericParamDefKind::Type { .. }, ..
169-
}) = generics.opt_param_at(n as usize, tcx)
167+
if let &ty::GenericParamDef { name, kind: ty::GenericParamDefKind::Type { .. }, .. } =
168+
generics.param_at(n as usize, tcx)
170169
{
171170
Ty::new_param(tcx, n, name)
172171
} else {

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+47-75
Original file line numberDiff line numberDiff line change
@@ -461,83 +461,55 @@ pub(super) fn explicit_predicates_of<'tcx>(
461461
}
462462
}
463463
} else {
464-
if matches!(def_kind, DefKind::AnonConst) && tcx.features().generic_const_exprs {
465-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
466-
let parent_def_id = tcx.hir().get_parent_item(hir_id);
467-
468-
if let Some(defaulted_param_def_id) =
469-
tcx.hir().opt_const_param_default_param_def_id(hir_id)
470-
{
471-
// In `generics_of` we set the generics' parent to be our parent's parent which means that
472-
// we lose out on the predicates of our actual parent if we dont return those predicates here.
473-
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
474-
//
475-
// struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
476-
// ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
477-
// ^^^ explicit_predicates_of on
478-
// parent item we dont have set as the
479-
// parent of generics returned by `generics_of`
480-
//
481-
// In the above code we want the anon const to have predicates in its param env for `T: Trait`
482-
// and we would be calling `explicit_predicates_of(Foo)` here
483-
let parent_preds = tcx.explicit_predicates_of(parent_def_id);
484-
485-
// If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
486-
// will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
487-
// to #106994 is implemented.
488-
let filtered_predicates = parent_preds
489-
.predicates
490-
.into_iter()
491-
.filter(|(pred, _)| {
492-
if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
493-
match ct.kind() {
494-
ty::ConstKind::Param(param_const) => {
495-
let defaulted_param_idx = tcx
496-
.generics_of(parent_def_id)
497-
.param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
498-
param_const.index < defaulted_param_idx
499-
}
500-
_ => bug!(
501-
"`ConstArgHasType` in `predicates_of`\
502-
that isn't a `Param` const"
503-
),
464+
if matches!(def_kind, DefKind::AnonConst)
465+
&& tcx.features().generic_const_exprs
466+
&& let Some(defaulted_param_def_id) =
467+
tcx.hir().opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
468+
{
469+
// In `generics_of` we set the generics' parent to be our parent's parent which means that
470+
// we lose out on the predicates of our actual parent if we dont return those predicates here.
471+
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
472+
//
473+
// struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
474+
// ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
475+
// ^^^ explicit_predicates_of on
476+
// parent item we dont have set as the
477+
// parent of generics returned by `generics_of`
478+
//
479+
// In the above code we want the anon const to have predicates in its param env for `T: Trait`
480+
// and we would be calling `explicit_predicates_of(Foo)` here
481+
let parent_def_id = tcx.local_parent(def_id);
482+
let parent_preds = tcx.explicit_predicates_of(parent_def_id);
483+
484+
// If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
485+
// will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
486+
// to #106994 is implemented.
487+
let filtered_predicates = parent_preds
488+
.predicates
489+
.into_iter()
490+
.filter(|(pred, _)| {
491+
if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
492+
match ct.kind() {
493+
ty::ConstKind::Param(param_const) => {
494+
let defaulted_param_idx = tcx
495+
.generics_of(parent_def_id)
496+
.param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
497+
param_const.index < defaulted_param_idx
504498
}
505-
} else {
506-
true
499+
_ => bug!(
500+
"`ConstArgHasType` in `predicates_of`\
501+
that isn't a `Param` const"
502+
),
507503
}
508-
})
509-
.cloned();
510-
return GenericPredicates {
511-
parent: parent_preds.parent,
512-
predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
513-
};
514-
}
515-
516-
let parent_def_kind = tcx.def_kind(parent_def_id);
517-
if matches!(parent_def_kind, DefKind::OpaqueTy) {
518-
// In `instantiate_identity` we inherit the predicates of our parent.
519-
// However, opaque types do not have a parent (see `gather_explicit_predicates_of`), which means
520-
// that we lose out on the predicates of our actual parent if we dont return those predicates here.
521-
//
522-
//
523-
// fn foo<T: Trait>() -> impl Iterator<Output = Another<{ <T as Trait>::ASSOC }> > { todo!() }
524-
// ^^^^^^^^^^^^^^^^^^^ the def id we are calling
525-
// explicit_predicates_of on
526-
//
527-
// In the above code we want the anon const to have predicates in its param env for `T: Trait`.
528-
// However, the anon const cannot inherit predicates from its parent since it's opaque.
529-
//
530-
// To fix this, we call `explicit_predicates_of` directly on `foo`, the parent's parent.
531-
532-
// In the above example this is `foo::{opaque#0}` or `impl Iterator`
533-
let parent_hir_id = tcx.local_def_id_to_hir_id(parent_def_id.def_id);
534-
535-
// In the above example this is the function `foo`
536-
let item_def_id = tcx.hir().get_parent_item(parent_hir_id);
537-
538-
// In the above code example we would be calling `explicit_predicates_of(foo)` here
539-
return tcx.explicit_predicates_of(item_def_id);
540-
}
504+
} else {
505+
true
506+
}
507+
})
508+
.cloned();
509+
return GenericPredicates {
510+
parent: parent_preds.parent,
511+
predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
512+
};
541513
}
542514
gather_explicit_predicates_of(tcx, def_id)
543515
}

compiler/rustc_hir_typeck/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ hir_typeck_rpit_change_return_type = you could change the return type to be a bo
137137
hir_typeck_rustcall_incorrect_args =
138138
functions with the "rust-call" ABI must take a single non-self tuple argument
139139
140+
hir_typeck_self_ctor_from_outer_item = can't reference `Self` constructor from outer item
141+
.label = the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
142+
.suggestion = replace `Self` with the actual type
143+
140144
hir_typeck_struct_expr_non_exhaustive =
141145
cannot create non-exhaustive {$what} using struct expression
142146

compiler/rustc_hir_typeck/src/errors.rs

+28
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,31 @@ pub enum SuggestBoxingForReturnImplTrait {
651651
ends: Vec<Span>,
652652
},
653653
}
654+
655+
#[derive(Diagnostic)]
656+
#[diag(hir_typeck_self_ctor_from_outer_item, code = E0401)]
657+
pub struct SelfCtorFromOuterItem {
658+
#[primary_span]
659+
pub span: Span,
660+
#[label]
661+
pub impl_span: Span,
662+
#[subdiagnostic]
663+
pub sugg: Option<ReplaceWithName>,
664+
}
665+
666+
#[derive(LintDiagnostic)]
667+
#[diag(hir_typeck_self_ctor_from_outer_item)]
668+
pub struct SelfCtorFromOuterItemLint {
669+
#[label]
670+
pub impl_span: Span,
671+
#[subdiagnostic]
672+
pub sugg: Option<ReplaceWithName>,
673+
}
674+
675+
#[derive(Subdiagnostic)]
676+
#[suggestion(hir_typeck_suggestion, code = "{name}", applicability = "machine-applicable")]
677+
pub struct ReplaceWithName {
678+
#[primary_span]
679+
pub span: Span,
680+
pub name: String,
681+
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::callee::{self, DeferredCallResolution};
2-
use crate::errors::CtorIsPrivate;
2+
use crate::errors::{self, CtorIsPrivate};
33
use crate::method::{self, MethodCallee, SelfSource};
44
use crate::rvalue_scopes;
55
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};
@@ -21,6 +21,7 @@ use rustc_hir_analysis::hir_ty_lowering::{
2121
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
2222
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
2323
use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
24+
use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
2425
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
2526
use rustc_middle::ty::error::TypeError;
2627
use rustc_middle::ty::fold::TypeFoldable;
@@ -1156,6 +1157,43 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11561157
span,
11571158
tcx.at(span).type_of(impl_def_id).instantiate_identity(),
11581159
);
1160+
1161+
// Firstly, check that this SelfCtor even comes from the item we're currently
1162+
// typechecking. This can happen because we never validated the resolution of
1163+
// SelfCtors, and when we started doing so, we noticed regressions. After
1164+
// sufficiently long time, we can remove this check and turn it into a hard
1165+
// error in `validate_res_from_ribs` -- it's just difficult to tell whether the
1166+
// self type has any generic types during rustc_resolve, which is what we use
1167+
// to determine if this is a hard error or warning.
1168+
if std::iter::successors(Some(self.body_id.to_def_id()), |def_id| {
1169+
self.tcx.generics_of(def_id).parent
1170+
})
1171+
.all(|def_id| def_id != impl_def_id)
1172+
{
1173+
let sugg = ty.normalized.ty_adt_def().map(|def| errors::ReplaceWithName {
1174+
span: path_span,
1175+
name: self.tcx.item_name(def.did()).to_ident_string(),
1176+
});
1177+
if ty.raw.has_param() {
1178+
let guar = self.tcx.dcx().emit_err(errors::SelfCtorFromOuterItem {
1179+
span: path_span,
1180+
impl_span: tcx.def_span(impl_def_id),
1181+
sugg,
1182+
});
1183+
return (Ty::new_error(self.tcx, guar), res);
1184+
} else {
1185+
self.tcx.emit_node_span_lint(
1186+
SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
1187+
hir_id,
1188+
path_span,
1189+
errors::SelfCtorFromOuterItemLint {
1190+
impl_span: tcx.def_span(impl_def_id),
1191+
sugg,
1192+
},
1193+
);
1194+
}
1195+
}
1196+
11591197
match ty.normalized.ty_adt_def() {
11601198
Some(adt_def) if adt_def.has_ctor() => {
11611199
let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap();

compiler/rustc_hir_typeck/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,17 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
235235
if let Some(item) = tcx.opt_associated_item(def_id.into())
236236
&& let ty::AssocKind::Const = item.kind
237237
&& let ty::ImplContainer = item.container
238-
&& let Some(trait_item) = item.trait_item_def_id
238+
&& let Some(trait_item_def_id) = item.trait_item_def_id
239239
{
240-
let args =
241-
tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
242-
Some(tcx.type_of(trait_item).instantiate(tcx, args))
240+
let impl_def_id = item.container_id(tcx);
241+
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
242+
let args = ty::GenericArgs::identity_for_item(tcx, def_id).rebase_onto(
243+
tcx,
244+
impl_def_id,
245+
impl_trait_ref.args,
246+
);
247+
tcx.check_args_compatible(trait_item_def_id, args)
248+
.then(|| tcx.type_of(trait_item_def_id).instantiate(tcx, args))
243249
} else {
244250
Some(fcx.next_ty_var(span))
245251
}

compiler/rustc_hir_typeck/src/op.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
381381
let maybe_missing_semi = self.check_for_missing_semi(expr, &mut err);
382382

383383
// We defer to the later error produced by `check_lhs_assignable`.
384-
// We only downgrade this if it's the LHS, though.
384+
// We only downgrade this if it's the LHS, though, and if this is a
385+
// valid assignment statement.
385386
if maybe_missing_semi
386387
&& let hir::Node::Expr(parent) = self.tcx.parent_hir_node(expr.hir_id)
387388
&& let hir::ExprKind::Assign(lhs, _, _) = parent.kind
389+
&& let hir::Node::Stmt(stmt) = self.tcx.parent_hir_node(parent.hir_id)
390+
&& let hir::StmtKind::Expr(_) | hir::StmtKind::Semi(_) = stmt.kind
388391
&& lhs.hir_id == expr.hir_id
389392
{
390393
err.downgrade_to_delayed_bug();

compiler/rustc_incremental/src/persist/load.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
116116

117117
if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
118118
// Decode the list of work_products
119-
let Ok(mut work_product_decoder) =
120-
MemDecoder::new(&work_products_data[..], start_pos)
119+
let Ok(mut work_product_decoder) = MemDecoder::new(&work_products_data[..], start_pos)
121120
else {
122121
sess.dcx().emit_warn(errors::CorruptFile { path: &work_products_path });
123122
return LoadResult::DataOutOfDate;

0 commit comments

Comments
 (0)