Skip to content

Commit d27207d

Browse files
committed
Auto merge of rust-lang#153919 - matthiaskrgr:rollup-UnSAPBk, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#153870 (docs: remove stale reference to `check_let_chain`) - rust-lang#153881 (Provide more context on type errors in const context) - rust-lang#153887 (Turn label into structured suggestion for `.as_ref()` and `.as_mut()`) - rust-lang#153897 (Use less `#[macro_use]` in the query system) - rust-lang#153914 (add test for param-env shadowing) - rust-lang#153917 (compiletest: show rustdoc logs when `--no-capture`)
2 parents f125037 + 06dcfe9 commit d27207d

104 files changed

Lines changed: 731 additions & 174 deletions

File tree

Some content is hidden

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

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
13771377
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
13781378
});
13791379
if is_option_or_result && maybe_reinitialized_locations_is_empty {
1380-
err.subdiagnostic(CaptureReasonLabel::BorrowContent { var_span });
1380+
err.subdiagnostic(CaptureReasonLabel::BorrowContent {
1381+
var_span: var_span.shrink_to_hi(),
1382+
});
13811383
}
13821384
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
13831385
let ty = moved_place.ty(self.body, tcx).ty;

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,18 @@ pub(crate) enum CaptureReasonLabel<'a> {
456456
is_move_msg: bool,
457457
is_loop_message: bool,
458458
},
459-
#[label("help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents")]
459+
#[suggestion(
460+
"consider calling `.as_ref()` to borrow the value's contents",
461+
applicability = "maybe-incorrect",
462+
code = ".as_ref()",
463+
style = "verbose"
464+
)]
465+
#[suggestion(
466+
"consider calling `.as_mut()` to mutably borrow the value's contents",
467+
applicability = "maybe-incorrect",
468+
code = ".as_mut()",
469+
style = "verbose"
470+
)]
460471
BorrowContent {
461472
#[primary_span]
462473
var_span: Span,

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use coercion::can_coerce;
4141
use fn_ctxt::FnCtxt;
4242
use rustc_data_structures::unord::UnordSet;
4343
use rustc_errors::codes::*;
44-
use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
44+
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, pluralize, struct_span_code_err};
4545
use rustc_hir as hir;
4646
use rustc_hir::def::{DefKind, Res};
4747
use rustc_hir::{HirId, HirIdMap, Node};
@@ -204,7 +204,9 @@ fn typeck_with_inspect<'tcx>(
204204
);
205205
}
206206

207-
fcx.check_expr_coercible_to_type(body.value, expected_type, None);
207+
fcx.check_expr_coercible_to_type_or_error(body.value, expected_type, None, |err, _| {
208+
extend_err_with_const_context(err, tcx, node, expected_type);
209+
});
208210

209211
fcx.write_ty(id, expected_type);
210212
};
@@ -274,6 +276,126 @@ fn typeck_with_inspect<'tcx>(
274276
typeck_results
275277
}
276278

279+
fn extend_err_with_const_context(
280+
err: &mut Diag<'_>,
281+
tcx: TyCtxt<'_>,
282+
node: hir::Node<'_>,
283+
expected_ty: Ty<'_>,
284+
) {
285+
match node {
286+
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), .. })
287+
| hir::Node::TraitItem(hir::TraitItem {
288+
kind: hir::TraitItemKind::Const(ty, _, _), ..
289+
}) => {
290+
// Point at the `Type` in `const NAME: Type = value;`.
291+
err.span_label(ty.span, "expected because of the type of the associated constant");
292+
}
293+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, ty, _), .. }) => {
294+
// Point at the `Type` in `const NAME: Type = value;`.
295+
err.span_label(ty.span, "expected because of the type of the constant");
296+
}
297+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(_, _, ty, _), .. }) => {
298+
// Point at the `Type` in `static NAME: Type = value;`.
299+
err.span_label(ty.span, "expected because of the type of the static");
300+
}
301+
hir::Node::AnonConst(anon)
302+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
303+
&& let hir::Node::Ty(parent) = tcx.parent_hir_node(parent.hir_id)
304+
&& let hir::TyKind::Array(_ty, _len) = parent.kind =>
305+
{
306+
// `[type; len]` in type context.
307+
err.note("array length can only be `usize`");
308+
}
309+
hir::Node::AnonConst(anon)
310+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
311+
&& let hir::Node::Expr(parent) = tcx.parent_hir_node(parent.hir_id)
312+
&& let hir::ExprKind::Repeat(_ty, _len) = parent.kind =>
313+
{
314+
// `[type; len]` in expr context.
315+
err.note("array length can only be `usize`");
316+
}
317+
// FIXME: support method calls too.
318+
hir::Node::AnonConst(anon)
319+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
320+
&& let hir::Node::Expr(expr) = tcx.parent_hir_node(parent.hir_id)
321+
&& let hir::ExprKind::Path(path) = expr.kind
322+
&& let hir::QPath::Resolved(_, path) = path
323+
&& let Res::Def(_, def_id) = path.res =>
324+
{
325+
// `foo<N>()` in expression context, point at `foo`'s const parameter.
326+
if let Some(i) =
327+
path.segments.iter().last().and_then(|segment| segment.args).and_then(|args| {
328+
args.args.iter().position(|arg| {
329+
matches!(arg, hir::GenericArg::Const(arg) if arg.hir_id == parent.hir_id)
330+
})
331+
})
332+
{
333+
let generics = tcx.generics_of(def_id);
334+
let param = &generics.param_at(i, tcx);
335+
let sp = tcx.def_span(param.def_id);
336+
err.span_note(sp, "expected because of the type of the const parameter");
337+
}
338+
}
339+
hir::Node::AnonConst(anon)
340+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
341+
&& let hir::Node::Ty(ty) = tcx.parent_hir_node(parent.hir_id)
342+
&& let hir::TyKind::Path(path) = ty.kind
343+
&& let hir::QPath::Resolved(_, path) = path
344+
&& let Res::Def(_, def_id) = path.res =>
345+
{
346+
// `Foo<N>` in type context, point at `Foo`'s const parameter.
347+
if let Some(i) =
348+
path.segments.iter().last().and_then(|segment| segment.args).and_then(|args| {
349+
args.args.iter().position(|arg| {
350+
matches!(arg, hir::GenericArg::Const(arg) if arg.hir_id == parent.hir_id)
351+
})
352+
})
353+
{
354+
let generics = tcx.generics_of(def_id);
355+
let param = &generics.param_at(i, tcx);
356+
let sp = tcx.def_span(param.def_id);
357+
err.span_note(sp, "expected because of the type of the const parameter");
358+
}
359+
}
360+
hir::Node::AnonConst(anon)
361+
if let hir::Node::Variant(_variant) = tcx.parent_hir_node(anon.hir_id) =>
362+
{
363+
// FIXME: point at `repr` when present in the type.
364+
err.note(
365+
"enum variant discriminant can only be of a primitive type compatible with the \
366+
enum's `repr`",
367+
);
368+
}
369+
hir::Node::AnonConst(anon)
370+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
371+
&& let hir::Node::GenericParam(param) = tcx.parent_hir_node(parent.hir_id)
372+
&& let hir::GenericParamKind::Const { ty, .. } = param.kind =>
373+
{
374+
// `fn foo<const N: usize = ()>` point at the `usize`.
375+
err.span_label(ty.span, "expected because of the type of the const parameter");
376+
}
377+
hir::Node::AnonConst(anon)
378+
if let hir::Node::ConstArg(parent) = tcx.parent_hir_node(anon.hir_id)
379+
&& let hir::Node::TyPat(ty_pat) = tcx.parent_hir_node(parent.hir_id)
380+
&& let hir::Node::Ty(ty) = tcx.parent_hir_node(ty_pat.hir_id)
381+
&& let hir::TyKind::Pat(ty, _) = ty.kind =>
382+
{
383+
// Point at `char` in `pattern_type!(char is 1..=1)`.
384+
err.span_label(ty.span, "the pattern must match the type");
385+
}
386+
hir::Node::AnonConst(anon)
387+
if let hir::Node::Field(_) = tcx.parent_hir_node(anon.hir_id)
388+
&& let ty::Param(_) = expected_ty.kind() =>
389+
{
390+
err.note(
391+
"the type of default fields referencing type parameters can't be assumed inside \
392+
the struct defining them",
393+
);
394+
}
395+
_ => {}
396+
}
397+
}
398+
277399
fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Option<Ty<'tcx>> {
278400
let tcx = fcx.tcx;
279401
let def_id = fcx.body_id;

compiler/rustc_macros/src/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
498498
/// Higher-order macro that invokes the specified macro with (a) a list of all query
499499
/// signatures (including modifiers), and (b) a list of non-query names. This allows
500500
/// multiple simpler macros to each have access to these lists.
501-
#[macro_export]
502-
macro_rules! rustc_with_all_queries {
501+
#[rustc_macro_transparency = "semiopaque"] // Use `macro_rules!` hygiene.
502+
pub macro rustc_with_all_queries {
503503
(
504504
// The macro to invoke once, on all queries and non-queries.
505505
$macro:ident!

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ macro_rules! define_dep_nodes {
331331
}
332332

333333
// Create various data structures for each query, and also for a few things that aren't queries.
334-
rustc_with_all_queries! { define_dep_nodes! }
334+
crate::queries::rustc_with_all_queries! { define_dep_nodes! }
335335

336336
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
337337
// Be very careful changing this type signature!

compiler/rustc_middle/src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ mod macros;
6767

6868
#[macro_use]
6969
pub mod arena;
70+
71+
pub mod dep_graph;
7072
pub mod error;
7173
pub mod hir;
7274
pub mod hooks;
@@ -76,18 +78,13 @@ pub mod lint;
7678
pub mod metadata;
7779
pub mod middle;
7880
pub mod mir;
81+
pub mod queries;
82+
pub mod query;
7983
pub mod thir;
8084
pub mod traits;
8185
pub mod ty;
8286
pub mod util;
8387
pub mod verify_ich;
8488

85-
#[macro_use]
86-
pub mod query;
87-
#[macro_use]
88-
pub mod queries;
89-
#[macro_use]
90-
pub mod dep_graph;
91-
9289
// Allows macros to refer to this crate as `::rustc_middle`
9390
extern crate self as rustc_middle;

compiler/rustc_middle/src/queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use crate::mir::mono::{
101101
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
102102
};
103103
use crate::query::describe_as_module;
104+
use crate::query::plumbing::{define_callbacks, query_helper_param_ty};
104105
use crate::traits::query::{
105106
CanonicalAliasGoal, CanonicalDropckOutlivesGoal, CanonicalImpliedOutlivesBoundsGoal,
106107
CanonicalMethodAutoderefStepsGoal, CanonicalPredicateGoal, CanonicalTypeOpAscribeUserTypeGoal,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ pub mod erase;
1717
pub(crate) mod inner;
1818
mod job;
1919
mod keys;
20+
pub(crate) mod modifiers;
2021
pub mod on_disk_cache;
21-
#[macro_use]
2222
pub mod plumbing;
23-
pub(crate) mod modifiers;
2423
mod stack;
2524

2625
pub fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> String {

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ macro_rules! define_callbacks {
630630
};
631631
}
632632

633+
// Re-export `macro_rules!` macros as normal items, so that they can be imported normally.
634+
pub(crate) use define_callbacks;
635+
pub(crate) use query_helper_param_ty;
636+
633637
mod sealed {
634638
use rustc_hir::def_id::{LocalModDefId, ModDefId};
635639

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ struct MatchVisitor<'p, 'tcx> {
101101
error: Result<(), ErrorGuaranteed>,
102102
}
103103

104-
// Visitor for a thir body. This calls `check_match`, `check_let` and `check_let_chain` as
105-
// appropriate.
104+
// Visitor for a thir body. This calls `check_match` and `check_let` as appropriate.
106105
impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
107106
fn thir(&self) -> &'p Thir<'tcx> {
108107
self.thir

0 commit comments

Comments
 (0)