Skip to content

Commit 0b77301

Browse files
committed
Auto merge of rust-lang#120283 - fmease:rollup-rk0f6r5, r=fmease
Rollup of 9 pull requests Successful merges: - rust-lang#112806 (Small code improvements in `collect_intra_doc_links.rs`) - rust-lang#119766 (Split tait and impl trait in assoc items logic) - rust-lang#120139 (Do not normalize closure signature when building `FnOnce` shim) - rust-lang#120160 (Manually implement derived `NonZero` traits.) - rust-lang#120171 (Fix assume and assert in jump threading) - rust-lang#120183 (Add `#[coverage(off)]` to closures introduced by `#[test]` and `#[bench]`) - rust-lang#120195 (add several resolution test cases) - rust-lang#120259 (Split Diagnostics for Uncommon Codepoints: Add List to Display Characters Involved) - rust-lang#120261 (Provide structured suggestion to use trait objects in some cases of `if` arm type divergence) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5d3d347 + 3f2f8ee commit 0b77301

File tree

87 files changed

+1606
-226
lines changed

Some content is hidden

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

87 files changed

+1606
-226
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![doc(rust_logo)]
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(array_windows)]
9+
#![feature(assert_matches)]
910
#![feature(box_patterns)]
1011
#![feature(decl_macro)]
1112
#![feature(if_let_guard)]

compiler/rustc_builtin_macros/src/test.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Level};
99
use rustc_expand::base::*;
1010
use rustc_span::symbol::{sym, Ident, Symbol};
1111
use rustc_span::{ErrorGuaranteed, FileNameDisplayPreference, Span};
12+
use std::assert_matches::assert_matches;
1213
use std::iter;
1314
use thin_vec::{thin_vec, ThinVec};
1415

@@ -182,6 +183,16 @@ pub fn expand_test_or_bench(
182183
// creates $name: $expr
183184
let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
184185

186+
// Adds `#[coverage(off)]` to a closure, so it won't be instrumented in
187+
// `-Cinstrument-coverage` builds.
188+
// This requires `#[allow_internal_unstable(coverage_attribute)]` on the
189+
// corresponding macro declaration in `core::macros`.
190+
let coverage_off = |mut expr: P<ast::Expr>| {
191+
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
192+
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
193+
expr
194+
};
195+
185196
let test_fn = if is_bench {
186197
// A simple ident for a lambda
187198
let b = Ident::from_str_and_span("b", attr_sp);
@@ -190,8 +201,9 @@ pub fn expand_test_or_bench(
190201
sp,
191202
cx.expr_path(test_path("StaticBenchFn")),
192203
thin_vec![
204+
// #[coverage(off)]
193205
// |b| self::test::assert_test_result(
194-
cx.lambda1(
206+
coverage_off(cx.lambda1(
195207
sp,
196208
cx.expr_call(
197209
sp,
@@ -206,16 +218,17 @@ pub fn expand_test_or_bench(
206218
],
207219
),
208220
b,
209-
), // )
221+
)), // )
210222
],
211223
)
212224
} else {
213225
cx.expr_call(
214226
sp,
215227
cx.expr_path(test_path("StaticTestFn")),
216228
thin_vec![
229+
// #[coverage(off)]
217230
// || {
218-
cx.lambda0(
231+
coverage_off(cx.lambda0(
219232
sp,
220233
// test::assert_test_result(
221234
cx.expr_call(
@@ -230,7 +243,7 @@ pub fn expand_test_or_bench(
230243
), // )
231244
],
232245
), // }
233-
), // )
246+
)), // )
234247
],
235248
)
236249
};

compiler/rustc_codegen_cranelift/src/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ fn codegen_stmt<'tcx>(
682682
args,
683683
ty::ClosureKind::FnOnce,
684684
)
685-
.expect("failed to normalize and resolve closure during codegen")
686685
.polymorphize(fx.tcx);
687686
let func_ref = fx.get_function_ref(instance);
688687
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
435435
args,
436436
ty::ClosureKind::FnOnce,
437437
)
438-
.expect("failed to normalize and resolve closure during codegen")
439438
.polymorphize(bx.cx().tcx());
440439
OperandValue::Immediate(bx.cx().get_fn_addr(instance))
441440
}

compiler/rustc_const_eval/src/interpret/cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
117117
def_id,
118118
args,
119119
ty::ClosureKind::FnOnce,
120-
)
121-
.ok_or_else(|| err_inval!(TooGeneric))?;
120+
);
122121
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
123122
self.write_pointer(fn_ptr, dest)?;
124123
}

compiler/rustc_errors/src/diagnostic_impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ impl IntoDiagnosticArg for char {
110110
}
111111
}
112112

113+
impl IntoDiagnosticArg for Vec<char> {
114+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
115+
DiagnosticArgValue::StrListSepByAnd(
116+
self.into_iter().map(|c| Cow::Owned(format!("{c:?}"))).collect(),
117+
)
118+
}
119+
}
120+
113121
impl IntoDiagnosticArg for Symbol {
114122
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
115123
self.to_ident_string().into_diagnostic_arg()

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,13 @@ pub(super) fn type_of_opaque(
530530
Ok(ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id) {
531531
Node::Item(item) => match item.kind {
532532
ItemKind::OpaqueTy(OpaqueTy {
533-
origin: hir::OpaqueTyOrigin::TyAlias { .. },
533+
origin: hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false },
534534
..
535535
}) => opaque::find_opaque_ty_constraints_for_tait(tcx, def_id),
536+
ItemKind::OpaqueTy(OpaqueTy {
537+
origin: hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: true },
538+
..
539+
}) => opaque::find_opaque_ty_constraints_for_impl_trait_in_assoc_type(tcx, def_id),
536540
// Opaque types desugared from `impl Trait`.
537541
ItemKind::OpaqueTy(&OpaqueTy {
538542
origin:

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+72-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,60 @@ pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed>
2323
res
2424
}
2525

26+
/// Checks "defining uses" of opaque `impl Trait` in associated types.
27+
/// These can only be defined by associated items of the same trait.
28+
#[instrument(skip(tcx), level = "debug")]
29+
pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
30+
tcx: TyCtxt<'_>,
31+
def_id: LocalDefId,
32+
) -> Ty<'_> {
33+
let mut parent_def_id = def_id;
34+
while tcx.def_kind(parent_def_id) == def::DefKind::OpaqueTy {
35+
// Account for `type Alias = impl Trait<Foo = impl Trait>;` (#116031)
36+
parent_def_id = tcx.local_parent(parent_def_id);
37+
}
38+
let impl_def_id = tcx.local_parent(parent_def_id);
39+
match tcx.def_kind(impl_def_id) {
40+
DefKind::Impl { .. } => {}
41+
other => bug!("invalid impl trait in assoc type parent: {other:?}"),
42+
}
43+
44+
let mut locator = TaitConstraintLocator { def_id, tcx, found: None, typeck_types: vec![] };
45+
46+
for &assoc_id in tcx.associated_item_def_ids(impl_def_id) {
47+
let assoc = tcx.associated_item(assoc_id);
48+
match assoc.kind {
49+
ty::AssocKind::Const | ty::AssocKind::Fn => {
50+
locator.check(assoc_id.expect_local(), ImplTraitSource::AssocTy)
51+
}
52+
// Associated types don't have bodies, so they can't constrain hidden types
53+
ty::AssocKind::Type => {}
54+
}
55+
}
56+
57+
if let Some(hidden) = locator.found {
58+
// Only check against typeck if we didn't already error
59+
if !hidden.ty.references_error() {
60+
for concrete_type in locator.typeck_types {
61+
if concrete_type.ty != tcx.erase_regions(hidden.ty)
62+
&& !(concrete_type, hidden).references_error()
63+
{
64+
hidden.report_mismatch(&concrete_type, def_id, tcx).emit();
65+
}
66+
}
67+
}
68+
69+
hidden.ty
70+
} else {
71+
let reported = tcx.dcx().emit_err(UnconstrainedOpaqueType {
72+
span: tcx.def_span(def_id),
73+
name: tcx.item_name(parent_def_id.to_def_id()),
74+
what: "impl",
75+
});
76+
Ty::new_error(tcx, reported)
77+
}
78+
}
79+
2680
/// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions
2781
/// laid for "higher-order pattern unification".
2882
/// This ensures that inference is tractable.
@@ -128,9 +182,15 @@ struct TaitConstraintLocator<'tcx> {
128182
typeck_types: Vec<ty::OpaqueHiddenType<'tcx>>,
129183
}
130184

185+
#[derive(Debug)]
186+
enum ImplTraitSource {
187+
AssocTy,
188+
TyAlias,
189+
}
190+
131191
impl TaitConstraintLocator<'_> {
132192
#[instrument(skip(self), level = "debug")]
133-
fn check(&mut self, item_def_id: LocalDefId) {
193+
fn check(&mut self, item_def_id: LocalDefId, source: ImplTraitSource) {
134194
// Don't try to check items that cannot possibly constrain the type.
135195
if !self.tcx.has_typeck_results(item_def_id) {
136196
debug!("no constraint: no typeck results");
@@ -182,7 +242,13 @@ impl TaitConstraintLocator<'_> {
182242
continue;
183243
}
184244
constrained = true;
185-
if !self.tcx.opaque_types_defined_by(item_def_id).contains(&self.def_id) {
245+
let opaque_types_defined_by = match source {
246+
ImplTraitSource::AssocTy => {
247+
self.tcx.impl_trait_in_assoc_types_defined_by(item_def_id)
248+
}
249+
ImplTraitSource::TyAlias => self.tcx.opaque_types_defined_by(item_def_id),
250+
};
251+
if !opaque_types_defined_by.contains(&self.def_id) {
186252
self.tcx.dcx().emit_err(TaitForwardCompat {
187253
span: hidden_type.span,
188254
item_span: self
@@ -240,29 +306,29 @@ impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> {
240306
}
241307
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
242308
if let hir::ExprKind::Closure(closure) = ex.kind {
243-
self.check(closure.def_id);
309+
self.check(closure.def_id, ImplTraitSource::TyAlias);
244310
}
245311
intravisit::walk_expr(self, ex);
246312
}
247313
fn visit_item(&mut self, it: &'tcx Item<'tcx>) {
248314
trace!(?it.owner_id);
249315
// The opaque type itself or its children are not within its reveal scope.
250316
if it.owner_id.def_id != self.def_id {
251-
self.check(it.owner_id.def_id);
317+
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
252318
intravisit::walk_item(self, it);
253319
}
254320
}
255321
fn visit_impl_item(&mut self, it: &'tcx ImplItem<'tcx>) {
256322
trace!(?it.owner_id);
257323
// The opaque type itself or its children are not within its reveal scope.
258324
if it.owner_id.def_id != self.def_id {
259-
self.check(it.owner_id.def_id);
325+
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
260326
intravisit::walk_impl_item(self, it);
261327
}
262328
}
263329
fn visit_trait_item(&mut self, it: &'tcx TraitItem<'tcx>) {
264330
trace!(?it.owner_id);
265-
self.check(it.owner_id.def_id);
331+
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
266332
intravisit::walk_trait_item(self, it);
267333
}
268334
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {

compiler/rustc_hir_typeck/src/demand.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4444
|| self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty)
4545
|| self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty)
4646
|| self.suggest_no_capture_closure(err, expected, expr_ty)
47-
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
47+
|| self.suggest_boxing_when_appropriate(
48+
err,
49+
expr.peel_blocks().span,
50+
expr.hir_id,
51+
expected,
52+
expr_ty,
53+
)
4854
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
4955
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
5056
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)

0 commit comments

Comments
 (0)