Skip to content

Commit 1716932

Browse files
committed
Auto merge of #109130 - matthiaskrgr:rollup-dm3jza6, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #108722 (Support for Fuchsia RISC-V target) - #108880 (Remove tests/ui/impl-trait/in-trait/new-lowering-strategy in favor of using revisions on existing tests) - #108909 (Fix object safety checks for new RPITITs) - #108915 (Remove some direct calls to local_def_id_to_hir_id on diagnostics) - #108923 (Make fns from other crates with RPITIT work for -Zlower-impl-trait-in-trait-to-assoc-ty) - #109101 (Fall back to old metadata computation when type references errors) - #109105 (Don't ICE for late-bound consts across `AnonConstBoundary`) - #109110 (Don't codegen impossible to satisfy impls) - #109116 (Emit diagnostic when calling methods on the unit type in method chains) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2e7034e + b17ee10 commit 1716932

File tree

84 files changed

+612
-106
lines changed

Some content is hidden

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

84 files changed

+612
-106
lines changed

compiler/rustc_hir/src/definitions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,12 @@ impl DefPathData {
404404
match *self {
405405
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
406406

407+
// We use this name when collecting `ModChild`s.
408+
// FIXME this could probably be removed with some refactoring to the name resolver.
409+
ImplTraitAssocTy => Some(kw::Empty),
410+
407411
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
408-
| ImplTrait | ImplTraitAssocTy => None,
412+
| ImplTrait => None,
409413
}
410414
}
411415

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14401440
tcx.associated_items(pred.def_id())
14411441
.in_definition_order()
14421442
.filter(|item| item.kind == ty::AssocKind::Type)
1443+
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
14431444
.map(|item| item.def_id),
14441445
);
14451446
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1427,25 +1427,25 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14271427
if let ResolvedArg::LateBound(..) = def && crossed_anon_const {
14281428
let use_span = self.tcx.hir().span(hir_id);
14291429
let def_span = self.tcx.def_span(param_def_id);
1430-
match self.tcx.def_kind(param_def_id) {
1430+
let guar = match self.tcx.def_kind(param_def_id) {
14311431
DefKind::ConstParam => {
14321432
self.tcx.sess.emit_err(errors::CannotCaptureLateBoundInAnonConst::Const {
14331433
use_span,
14341434
def_span,
1435-
});
1435+
})
14361436
}
14371437
DefKind::TyParam => {
14381438
self.tcx.sess.emit_err(errors::CannotCaptureLateBoundInAnonConst::Type {
14391439
use_span,
14401440
def_span,
1441-
});
1441+
})
14421442
}
14431443
_ => unreachable!(),
1444-
}
1445-
return;
1444+
};
1445+
self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1446+
} else {
1447+
self.map.defs.insert(hir_id, def);
14461448
}
1447-
1448-
self.map.defs.insert(hir_id, def);
14491449
return;
14501450
}
14511451

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8383
self.annotate_expected_due_to_let_ty(err, expr, error);
8484
self.emit_type_mismatch_suggestions(err, expr, expr_ty, expected, expected_ty_expr, error);
8585
self.note_type_is_not_clone(err, expected, expr_ty, expr);
86-
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
86+
self.note_internal_mutation_in_method(err, expr, Some(expected), expr_ty);
8787
self.check_for_range_as_method_call(err, expr, expr_ty, expected);
8888
self.check_for_binding_assigned_block_without_tail_expression(err, expr, expr_ty, expected);
8989
self.check_wrong_return_type_due_to_generic_arg(err, expr, expr_ty);

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+56-25
Original file line numberDiff line numberDiff line change
@@ -950,44 +950,75 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
950950
&self,
951951
err: &mut Diagnostic,
952952
expr: &hir::Expr<'_>,
953-
expected: Ty<'tcx>,
953+
expected: Option<Ty<'tcx>>,
954954
found: Ty<'tcx>,
955955
) {
956956
if found != self.tcx.types.unit {
957957
return;
958958
}
959-
if let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind {
960-
if self
961-
.typeck_results
959+
960+
let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind else {
961+
return;
962+
};
963+
964+
let rcvr_has_the_expected_type = self
965+
.typeck_results
966+
.borrow()
967+
.expr_ty_adjusted_opt(rcvr)
968+
.and_then(|ty| expected.map(|expected_ty| expected_ty.peel_refs() == ty.peel_refs()))
969+
.unwrap_or(false);
970+
971+
let prev_call_mutates_and_returns_unit = || {
972+
self.typeck_results
962973
.borrow()
963-
.expr_ty_adjusted_opt(rcvr)
964-
.map_or(true, |ty| expected.peel_refs() != ty.peel_refs())
965-
{
966-
return;
967-
}
968-
let mut sp = MultiSpan::from_span(path_segment.ident.span);
969-
sp.push_span_label(
970-
path_segment.ident.span,
971-
format!(
972-
"this call modifies {} in-place",
973-
match rcvr.kind {
974-
ExprKind::Path(QPath::Resolved(
975-
None,
976-
hir::Path { segments: [segment], .. },
977-
)) => format!("`{}`", segment.ident),
978-
_ => "its receiver".to_string(),
979-
}
980-
),
981-
);
974+
.type_dependent_def_id(expr.hir_id)
975+
.map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
976+
.and_then(|sig| sig.inputs_and_output.split_last())
977+
.map(|(output, inputs)| {
978+
output.is_unit()
979+
&& inputs
980+
.get(0)
981+
.and_then(|self_ty| self_ty.ref_mutability())
982+
.map_or(false, rustc_ast::Mutability::is_mut)
983+
})
984+
.unwrap_or(false)
985+
};
986+
987+
if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
988+
return;
989+
}
990+
991+
let mut sp = MultiSpan::from_span(path_segment.ident.span);
992+
sp.push_span_label(
993+
path_segment.ident.span,
994+
format!(
995+
"this call modifies {} in-place",
996+
match rcvr.kind {
997+
ExprKind::Path(QPath::Resolved(
998+
None,
999+
hir::Path { segments: [segment], .. },
1000+
)) => format!("`{}`", segment.ident),
1001+
_ => "its receiver".to_string(),
1002+
}
1003+
),
1004+
);
1005+
1006+
let modifies_rcvr_note =
1007+
format!("method `{}` modifies its receiver in-place", path_segment.ident);
1008+
if rcvr_has_the_expected_type {
9821009
sp.push_span_label(
9831010
rcvr.span,
9841011
"you probably want to use this value after calling the method...",
9851012
);
1013+
err.span_note(sp, &modifies_rcvr_note);
1014+
err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
1015+
} else if let ExprKind::MethodCall(..) = rcvr.kind {
9861016
err.span_note(
9871017
sp,
988-
&format!("method `{}` modifies its receiver in-place", path_segment.ident),
1018+
modifies_rcvr_note.clone() + ", it is not meant to be used in method chains.",
9891019
);
990-
err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
1020+
} else {
1021+
err.span_note(sp, &modifies_rcvr_note);
9911022
}
9921023
}
9931024

compiler/rustc_hir_typeck/src/method/suggest.rs

+7
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
416416
);
417417
probe.is_ok()
418418
});
419+
420+
self.note_internal_mutation_in_method(
421+
&mut err,
422+
rcvr_expr,
423+
expected.to_option(&self),
424+
rcvr_ty,
425+
);
419426
}
420427

421428
let mut custom_span_label = false;

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1356,13 +1356,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13561356
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
13571357
let tcx = self.tcx;
13581358

1359-
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
1360-
self.tables.impl_defaultness.set_some(def_id.index, ast_item.defaultness);
1359+
let defaultness = self.tcx.impl_defaultness(def_id.expect_local());
1360+
self.tables.impl_defaultness.set_some(def_id.index, defaultness);
13611361
let impl_item = self.tcx.associated_item(def_id);
13621362
self.tables.assoc_container.set_some(def_id.index, impl_item.container);
13631363

13641364
match impl_item.kind {
13651365
ty::AssocKind::Fn => {
1366+
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
13661367
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
13671368
self.tables.asyncness.set_some(def_id.index, sig.header.asyncness);
13681369
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));

compiler/rustc_middle/src/hir/map/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl<'hir> Map<'hir> {
316316
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
317317
#[inline]
318318
pub fn find_by_def_id(self, id: LocalDefId) -> Option<Node<'hir>> {
319-
self.find(self.local_def_id_to_hir_id(id))
319+
self.find(self.tcx.opt_local_def_id_to_hir_id(id)?)
320320
}
321321

322322
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
@@ -333,7 +333,7 @@ impl<'hir> Map<'hir> {
333333
}
334334

335335
pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
336-
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
336+
id.as_local().and_then(|id| self.find(self.tcx.opt_local_def_id_to_hir_id(id)?))
337337
}
338338

339339
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {

compiler/rustc_middle/src/ty/layout.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,11 @@ where
730730
*/
731731
};
732732

733-
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
733+
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
734+
// Projection eagerly bails out when the pointee references errors,
735+
// fall back to structurally deducing metadata.
736+
&& !pointee.references_error()
737+
{
734738
let metadata = tcx.normalize_erasing_regions(
735739
cx.param_env(),
736740
tcx.mk_projection(metadata_def_id, [pointee]),

compiler/rustc_monomorphize/src/collector.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,21 @@ fn create_mono_items_for_default_impls<'tcx>(
13261326
return;
13271327
}
13281328

1329+
// Unlike 'lazy' monomorphization that begins by collecting items transitively
1330+
// called by `main` or other global items, when eagerly monomorphizing impl
1331+
// items, we never actually check that the predicates of this impl are satisfied
1332+
// in a empty reveal-all param env (i.e. with no assumptions).
1333+
//
1334+
// Even though this impl has no substitutions, because we don't consider higher-
1335+
// ranked predicates such as `for<'a> &'a mut [u8]: Copy` to be trivially false,
1336+
// we must now check that the impl has no impossible-to-satisfy predicates.
1337+
if tcx.subst_and_check_impossible_predicates((
1338+
item.owner_id.to_def_id(),
1339+
&InternalSubsts::identity_for_item(tcx, item.owner_id.to_def_id()),
1340+
)) {
1341+
return;
1342+
}
1343+
13291344
let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) else {
13301345
return;
13311346
};

compiler/rustc_target/src/spec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ supported_targets! {
11151115
// FIXME(#106649): Remove aarch64-fuchsia in favor of aarch64-unknown-fuchsia
11161116
("aarch64-fuchsia", aarch64_fuchsia),
11171117
("aarch64-unknown-fuchsia", aarch64_unknown_fuchsia),
1118+
("riscv64gc-unknown-fuchsia", riscv64gc_unknown_fuchsia),
11181119
// FIXME(#106649): Remove x86_64-fuchsia in favor of x86_64-unknown-fuchsia
11191120
("x86_64-fuchsia", x86_64_fuchsia),
11201121
("x86_64-unknown-fuchsia", x86_64_unknown_fuchsia),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::spec::{CodeModel, SanitizerSet, Target, TargetOptions};
2+
3+
pub fn target() -> Target {
4+
Target {
5+
llvm_target: "riscv64gc-unknown-fuchsia".into(),
6+
pointer_width: 64,
7+
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".into(),
8+
arch: "riscv64".into(),
9+
options: TargetOptions {
10+
code_model: Some(CodeModel::Medium),
11+
cpu: "generic-rv64".into(),
12+
features: "+m,+a,+f,+d,+c".into(),
13+
llvm_abiname: "lp64d".into(),
14+
max_atomic_width: Some(64),
15+
supported_sanitizers: SanitizerSet::SHADOWCALLSTACK,
16+
..super::fuchsia_base::opts()
17+
},
18+
}
19+
}

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
144144
trait_ref: ty::PolyTraitRef<'tcx>,
145145
obligation: &PredicateObligation<'tcx>,
146146
) -> OnUnimplementedNote {
147+
if self.tcx.opt_rpitit_info(obligation.cause.body_id.to_def_id()).is_some() {
148+
return OnUnimplementedNote::default();
149+
}
150+
147151
let (def_id, substs) = self
148152
.impl_similar_to(trait_ref, obligation)
149153
.unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().substs));

compiler/rustc_trait_selection/src/traits/object_safety.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use super::{elaborate_predicates, elaborate_trait_ref};
1313
use crate::infer::TyCtxtInferExt;
1414
use crate::traits::query::evaluate_obligation::InferCtxtExt;
1515
use crate::traits::{self, Obligation, ObligationCause};
16-
use hir::def::DefKind;
1716
use rustc_errors::{DelayDm, FatalError, MultiSpan};
1817
use rustc_hir as hir;
1918
use rustc_hir::def_id::DefId;
@@ -157,6 +156,7 @@ fn object_safety_violations_for_trait(
157156
.in_definition_order()
158157
.filter(|item| item.kind == ty::AssocKind::Type)
159158
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
159+
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
160160
.map(|item| {
161161
let ident = item.ident(tcx);
162162
ObjectSafetyViolation::GAT(ident.name, ident.span)
@@ -854,7 +854,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
854854
}
855855
}
856856
ty::Alias(ty::Projection, ref data)
857-
if self.tcx.def_kind(data.def_id) == DefKind::ImplTraitPlaceholder =>
857+
if self.tcx.is_impl_trait_in_trait(data.def_id) =>
858858
{
859859
// We'll deny these later in their own pass
860860
ControlFlow::Continue(())
@@ -921,7 +921,7 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
921921
ty.skip_binder().walk().find_map(|arg| {
922922
if let ty::GenericArgKind::Type(ty) = arg.unpack()
923923
&& let ty::Alias(ty::Projection, proj) = ty.kind()
924-
&& tcx.def_kind(proj.def_id) == DefKind::ImplTraitPlaceholder
924+
&& tcx.is_impl_trait_in_trait(proj.def_id)
925925
{
926926
Some(MethodViolationCode::ReferencesImplTraitInTrait(tcx.def_span(proj.def_id)))
927927
} else {

compiler/rustc_ty_utils/src/assoc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ fn impl_associated_item_for_impl_trait_in_trait(
328328
// `opt_local_def_id_to_hir_id` with `None`.
329329
impl_assoc_ty.opt_local_def_id_to_hir_id(None);
330330

331+
// Copy span of the opaque.
332+
impl_assoc_ty.def_ident_span(Some(span));
333+
331334
impl_assoc_ty.associated_item(ty::AssocItem {
332335
name: kw::Empty,
333336
kind: ty::AssocKind::Type,
@@ -342,6 +345,9 @@ fn impl_associated_item_for_impl_trait_in_trait(
342345
// extra predicates to assume.
343346
impl_assoc_ty.param_env(tcx.param_env(impl_fn_def_id));
344347

348+
// Copy visility of the containing function.
349+
impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id));
350+
345351
// Copy impl_defaultness of the containing function.
346352
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
347353

compiler/rustc_ty_utils/src/layout.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ fn layout_of_uncached<'tcx>(
156156

157157
let unsized_part = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
158158

159-
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
159+
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
160+
// Projection eagerly bails out when the pointee references errors,
161+
// fall back to structurally deducing metadata.
162+
&& !pointee.references_error()
163+
{
160164
let metadata_ty = tcx.normalize_erasing_regions(
161165
param_env,
162166
tcx.mk_projection(metadata_def_id, [pointee]),

src/doc/rustc/src/platform-support.md

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ target | std | host | notes
295295
[`riscv32imac-unknown-xous-elf`](platform-support/riscv32imac-unknown-xous-elf.md) | ? | | RISC-V Xous (RV32IMAC ISA)
296296
`riscv32imc-esp-espidf` | ✓ | | RISC-V ESP-IDF
297297
`riscv64gc-unknown-freebsd` | | | RISC-V FreeBSD
298+
`riscv64gc-unknown-fuchsia` | | | RISC-V Fuchsia
298299
`riscv64gc-unknown-linux-musl` | | | RISC-V Linux (kernel 4.20, musl 1.2.0)
299300
[`riscv64gc-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | OpenBSD/riscv64
300301
`s390x-unknown-linux-musl` | | | S390x Linux (kernel 3.2, MUSL)

tests/ui/async-await/in-trait/async-associated-types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
22
// edition: 2021
3+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
4+
// revisions: current next
35

46
#![feature(async_fn_in_trait)]
57
#![feature(impl_trait_projections)]

tests/ui/async-await/in-trait/async-associated-types2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
22
// edition: 2021
3+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
4+
// revisions: current next
35

46
#![feature(async_fn_in_trait)]
57
#![feature(type_alias_impl_trait)]

tests/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr tests/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.current.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0053]: method `foo` has an incompatible type for trait
2-
--> $DIR/async-example-desugared-boxed-in-trait.rs:15:28
2+
--> $DIR/async-example-desugared-boxed-in-trait.rs:17:28
33
|
44
LL | async fn foo(&self) -> i32 {
55
| ^^^ expected `Pin<Box<dyn Future<Output = i32>>>`, found future
66
|
77
note: type in trait
8-
--> $DIR/async-example-desugared-boxed-in-trait.rs:11:22
8+
--> $DIR/async-example-desugared-boxed-in-trait.rs:13:22
99
|
1010
LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)