Skip to content

Commit

Permalink
Auto merge of rust-lang#109130 - matthiaskrgr:rollup-dm3jza6, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#108722 (Support for Fuchsia RISC-V target)
 - rust-lang#108880 (Remove tests/ui/impl-trait/in-trait/new-lowering-strategy in favor of using revisions on existing tests)
 - rust-lang#108909 (Fix object safety checks for new RPITITs)
 - rust-lang#108915 (Remove some direct calls to local_def_id_to_hir_id on diagnostics)
 - rust-lang#108923 (Make fns from other crates with RPITIT work for -Zlower-impl-trait-in-trait-to-assoc-ty)
 - rust-lang#109101 (Fall back to old metadata computation when type references errors)
 - rust-lang#109105 (Don't ICE for late-bound consts across `AnonConstBoundary`)
 - rust-lang#109110 (Don't codegen impossible to satisfy impls)
 - rust-lang#109116 (Emit diagnostic when calling methods on the unit type in method chains)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Mar 14, 2023
2 parents 2e7034e + b17ee10 commit 1716932
Show file tree
Hide file tree
Showing 84 changed files with 612 additions and 106 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,12 @@ impl DefPathData {
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),

// We use this name when collecting `ModChild`s.
// FIXME this could probably be removed with some refactoring to the name resolver.
ImplTraitAssocTy => Some(kw::Empty),

Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
| ImplTrait | ImplTraitAssocTy => None,
| ImplTrait => None,
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
tcx.associated_items(pred.def_id())
.in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Type)
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
.map(|item| item.def_id),
);
}
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,25 +1427,25 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
if let ResolvedArg::LateBound(..) = def && crossed_anon_const {
let use_span = self.tcx.hir().span(hir_id);
let def_span = self.tcx.def_span(param_def_id);
match self.tcx.def_kind(param_def_id) {
let guar = match self.tcx.def_kind(param_def_id) {
DefKind::ConstParam => {
self.tcx.sess.emit_err(errors::CannotCaptureLateBoundInAnonConst::Const {
use_span,
def_span,
});
})
}
DefKind::TyParam => {
self.tcx.sess.emit_err(errors::CannotCaptureLateBoundInAnonConst::Type {
use_span,
def_span,
});
})
}
_ => unreachable!(),
}
return;
};
self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
} else {
self.map.defs.insert(hir_id, def);
}

self.map.defs.insert(hir_id, def);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.annotate_expected_due_to_let_ty(err, expr, error);
self.emit_type_mismatch_suggestions(err, expr, expr_ty, expected, expected_ty_expr, error);
self.note_type_is_not_clone(err, expected, expr_ty, expr);
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
self.note_internal_mutation_in_method(err, expr, Some(expected), expr_ty);
self.check_for_range_as_method_call(err, expr, expr_ty, expected);
self.check_for_binding_assigned_block_without_tail_expression(err, expr, expr_ty, expected);
self.check_wrong_return_type_due_to_generic_arg(err, expr, expr_ty);
Expand Down
81 changes: 56 additions & 25 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,44 +950,75 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
err: &mut Diagnostic,
expr: &hir::Expr<'_>,
expected: Ty<'tcx>,
expected: Option<Ty<'tcx>>,
found: Ty<'tcx>,
) {
if found != self.tcx.types.unit {
return;
}
if let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind {
if self
.typeck_results

let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind else {
return;
};

let rcvr_has_the_expected_type = self
.typeck_results
.borrow()
.expr_ty_adjusted_opt(rcvr)
.and_then(|ty| expected.map(|expected_ty| expected_ty.peel_refs() == ty.peel_refs()))
.unwrap_or(false);

let prev_call_mutates_and_returns_unit = || {
self.typeck_results
.borrow()
.expr_ty_adjusted_opt(rcvr)
.map_or(true, |ty| expected.peel_refs() != ty.peel_refs())
{
return;
}
let mut sp = MultiSpan::from_span(path_segment.ident.span);
sp.push_span_label(
path_segment.ident.span,
format!(
"this call modifies {} in-place",
match rcvr.kind {
ExprKind::Path(QPath::Resolved(
None,
hir::Path { segments: [segment], .. },
)) => format!("`{}`", segment.ident),
_ => "its receiver".to_string(),
}
),
);
.type_dependent_def_id(expr.hir_id)
.map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
.and_then(|sig| sig.inputs_and_output.split_last())
.map(|(output, inputs)| {
output.is_unit()
&& inputs
.get(0)
.and_then(|self_ty| self_ty.ref_mutability())
.map_or(false, rustc_ast::Mutability::is_mut)
})
.unwrap_or(false)
};

if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
return;
}

let mut sp = MultiSpan::from_span(path_segment.ident.span);
sp.push_span_label(
path_segment.ident.span,
format!(
"this call modifies {} in-place",
match rcvr.kind {
ExprKind::Path(QPath::Resolved(
None,
hir::Path { segments: [segment], .. },
)) => format!("`{}`", segment.ident),
_ => "its receiver".to_string(),
}
),
);

let modifies_rcvr_note =
format!("method `{}` modifies its receiver in-place", path_segment.ident);
if rcvr_has_the_expected_type {
sp.push_span_label(
rcvr.span,
"you probably want to use this value after calling the method...",
);
err.span_note(sp, &modifies_rcvr_note);
err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
} else if let ExprKind::MethodCall(..) = rcvr.kind {
err.span_note(
sp,
&format!("method `{}` modifies its receiver in-place", path_segment.ident),
modifies_rcvr_note.clone() + ", it is not meant to be used in method chains.",
);
err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
} else {
err.span_note(sp, &modifies_rcvr_note);
}
}

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
probe.is_ok()
});

self.note_internal_mutation_in_method(
&mut err,
rcvr_expr,
expected.to_option(&self),
rcvr_ty,
);
}

let mut custom_span_label = false;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,13 +1356,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
let tcx = self.tcx;

let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
self.tables.impl_defaultness.set_some(def_id.index, ast_item.defaultness);
let defaultness = self.tcx.impl_defaultness(def_id.expect_local());
self.tables.impl_defaultness.set_some(def_id.index, defaultness);
let impl_item = self.tcx.associated_item(def_id);
self.tables.assoc_container.set_some(def_id.index, impl_item.container);

match impl_item.kind {
ty::AssocKind::Fn => {
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
self.tables.asyncness.set_some(def_id.index, sig.header.asyncness);
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<'hir> Map<'hir> {
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
#[inline]
pub fn find_by_def_id(self, id: LocalDefId) -> Option<Node<'hir>> {
self.find(self.local_def_id_to_hir_id(id))
self.find(self.tcx.opt_local_def_id_to_hir_id(id)?)
}

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

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

pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,11 @@ where
*/
};

let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
// Projection eagerly bails out when the pointee references errors,
// fall back to structurally deducing metadata.
&& !pointee.references_error()
{
let metadata = tcx.normalize_erasing_regions(
cx.param_env(),
tcx.mk_projection(metadata_def_id, [pointee]),
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,21 @@ fn create_mono_items_for_default_impls<'tcx>(
return;
}

// Unlike 'lazy' monomorphization that begins by collecting items transitively
// called by `main` or other global items, when eagerly monomorphizing impl
// items, we never actually check that the predicates of this impl are satisfied
// in a empty reveal-all param env (i.e. with no assumptions).
//
// Even though this impl has no substitutions, because we don't consider higher-
// ranked predicates such as `for<'a> &'a mut [u8]: Copy` to be trivially false,
// we must now check that the impl has no impossible-to-satisfy predicates.
if tcx.subst_and_check_impossible_predicates((
item.owner_id.to_def_id(),
&InternalSubsts::identity_for_item(tcx, item.owner_id.to_def_id()),
)) {
return;
}

let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) else {
return;
};
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ supported_targets! {
// FIXME(#106649): Remove aarch64-fuchsia in favor of aarch64-unknown-fuchsia
("aarch64-fuchsia", aarch64_fuchsia),
("aarch64-unknown-fuchsia", aarch64_unknown_fuchsia),
("riscv64gc-unknown-fuchsia", riscv64gc_unknown_fuchsia),
// FIXME(#106649): Remove x86_64-fuchsia in favor of x86_64-unknown-fuchsia
("x86_64-fuchsia", x86_64_fuchsia),
("x86_64-unknown-fuchsia", x86_64_unknown_fuchsia),
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_target/src/spec/riscv64gc_unknown_fuchsia.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::spec::{CodeModel, SanitizerSet, Target, TargetOptions};

pub fn target() -> Target {
Target {
llvm_target: "riscv64gc-unknown-fuchsia".into(),
pointer_width: 64,
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".into(),
arch: "riscv64".into(),
options: TargetOptions {
code_model: Some(CodeModel::Medium),
cpu: "generic-rv64".into(),
features: "+m,+a,+f,+d,+c".into(),
llvm_abiname: "lp64d".into(),
max_atomic_width: Some(64),
supported_sanitizers: SanitizerSet::SHADOWCALLSTACK,
..super::fuchsia_base::opts()
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_ref: ty::PolyTraitRef<'tcx>,
obligation: &PredicateObligation<'tcx>,
) -> OnUnimplementedNote {
if self.tcx.opt_rpitit_info(obligation.cause.body_id.to_def_id()).is_some() {
return OnUnimplementedNote::default();
}

let (def_id, substs) = self
.impl_similar_to(trait_ref, obligation)
.unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().substs));
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use super::{elaborate_predicates, elaborate_trait_ref};
use crate::infer::TyCtxtInferExt;
use crate::traits::query::evaluate_obligation::InferCtxtExt;
use crate::traits::{self, Obligation, ObligationCause};
use hir::def::DefKind;
use rustc_errors::{DelayDm, FatalError, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -157,6 +156,7 @@ fn object_safety_violations_for_trait(
.in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Type)
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
.map(|item| {
let ident = item.ident(tcx);
ObjectSafetyViolation::GAT(ident.name, ident.span)
Expand Down Expand Up @@ -854,7 +854,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
}
}
ty::Alias(ty::Projection, ref data)
if self.tcx.def_kind(data.def_id) == DefKind::ImplTraitPlaceholder =>
if self.tcx.is_impl_trait_in_trait(data.def_id) =>
{
// We'll deny these later in their own pass
ControlFlow::Continue(())
Expand Down Expand Up @@ -921,7 +921,7 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
ty.skip_binder().walk().find_map(|arg| {
if let ty::GenericArgKind::Type(ty) = arg.unpack()
&& let ty::Alias(ty::Projection, proj) = ty.kind()
&& tcx.def_kind(proj.def_id) == DefKind::ImplTraitPlaceholder
&& tcx.is_impl_trait_in_trait(proj.def_id)
{
Some(MethodViolationCode::ReferencesImplTraitInTrait(tcx.def_span(proj.def_id)))
} else {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ fn impl_associated_item_for_impl_trait_in_trait(
// `opt_local_def_id_to_hir_id` with `None`.
impl_assoc_ty.opt_local_def_id_to_hir_id(None);

// Copy span of the opaque.
impl_assoc_ty.def_ident_span(Some(span));

impl_assoc_ty.associated_item(ty::AssocItem {
name: kw::Empty,
kind: ty::AssocKind::Type,
Expand All @@ -342,6 +345,9 @@ fn impl_associated_item_for_impl_trait_in_trait(
// extra predicates to assume.
impl_assoc_ty.param_env(tcx.param_env(impl_fn_def_id));

// Copy visility of the containing function.
impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id));

// Copy impl_defaultness of the containing function.
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));

Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ fn layout_of_uncached<'tcx>(

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

let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
// Projection eagerly bails out when the pointee references errors,
// fall back to structurally deducing metadata.
&& !pointee.references_error()
{
let metadata_ty = tcx.normalize_erasing_regions(
param_env,
tcx.mk_projection(metadata_def_id, [pointee]),
Expand Down
1 change: 1 addition & 0 deletions src/doc/rustc/src/platform-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ target | std | host | notes
[`riscv32imac-unknown-xous-elf`](platform-support/riscv32imac-unknown-xous-elf.md) | ? | | RISC-V Xous (RV32IMAC ISA)
`riscv32imc-esp-espidf` | ✓ | | RISC-V ESP-IDF
`riscv64gc-unknown-freebsd` | | | RISC-V FreeBSD
`riscv64gc-unknown-fuchsia` | | | RISC-V Fuchsia
`riscv64gc-unknown-linux-musl` | | | RISC-V Linux (kernel 4.20, musl 1.2.0)
[`riscv64gc-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | OpenBSD/riscv64
`s390x-unknown-linux-musl` | | | S390x Linux (kernel 3.2, MUSL)
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/async-await/in-trait/async-associated-types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// check-pass
// edition: 2021
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
// revisions: current next

#![feature(async_fn_in_trait)]
#![feature(impl_trait_projections)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/async-await/in-trait/async-associated-types2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// check-pass
// edition: 2021
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
// revisions: current next

#![feature(async_fn_in_trait)]
#![feature(type_alias_impl_trait)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0053]: method `foo` has an incompatible type for trait
--> $DIR/async-example-desugared-boxed-in-trait.rs:15:28
--> $DIR/async-example-desugared-boxed-in-trait.rs:17:28
|
LL | async fn foo(&self) -> i32 {
| ^^^ expected `Pin<Box<dyn Future<Output = i32>>>`, found future
|
note: type in trait
--> $DIR/async-example-desugared-boxed-in-trait.rs:11:22
--> $DIR/async-example-desugared-boxed-in-trait.rs:13:22
|
LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading

0 comments on commit 1716932

Please sign in to comment.