Skip to content

Commit 2e43d06

Browse files
committed
Auto merge of #99636 - matthiaskrgr:rollup-yg0xxkx, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #99580 (Don't suggest unnameable generic arguments) - #99617 (Update mdbook) - #99631 (Use span_bug in case of unexpected rib kind) - #99632 (Fix typo/grammar in locator.rs doc comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 47ba935 + 7c5df1f commit 2e43d06

File tree

16 files changed

+95
-27
lines changed

16 files changed

+95
-27
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -2361,9 +2361,9 @@ dependencies = [
23612361

23622362
[[package]]
23632363
name = "mdbook"
2364-
version = "0.4.20"
2364+
version = "0.4.21"
23652365
source = "registry+https://github.com/rust-lang/crates.io-index"
2366-
checksum = "13cdad8057b09a519c6c63e6d7c93ea854f5d7fbfe284df864d5e1140d215a2d"
2366+
checksum = "23f3e133c6d515528745ffd3b9f0c7d975ae039f0b6abb099f2168daa2afb4f9"
23672367
dependencies = [
23682368
"ammonia",
23692369
"anyhow",

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::infer::type_variable::TypeVariableOriginKind;
1+
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
22
use crate::infer::InferCtxt;
33
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
44
use rustc_hir as hir;
@@ -8,12 +8,12 @@ use rustc_hir::def_id::DefId;
88
use rustc_hir::intravisit::{self, Visitor};
99
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
1010
use rustc_middle::hir::nested_filter;
11-
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
11+
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1212
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
1313
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
1414
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
1515
use rustc_middle::ty::{self, DefIdTree, InferConst};
16-
use rustc_middle::ty::{Ty, TyCtxt, TypeckResults};
16+
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeckResults};
1717
use rustc_span::symbol::{kw, Ident};
1818
use rustc_span::{BytePos, Span};
1919
use std::borrow::Cow;
@@ -407,11 +407,40 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
407407

408408
err.span_label(span, cannot_infer_msg);
409409

410-
let printer = fmt_printer(self, Namespace::TypeNS);
411-
let args = printer.comma_sep(generic_args.iter().copied()).unwrap().into_buffer();
410+
let args = fmt_printer(self, Namespace::TypeNS)
411+
.comma_sep(generic_args.iter().copied().map(|arg| {
412+
if arg.is_suggestable(self.tcx, true) {
413+
return arg;
414+
}
415+
416+
match arg.unpack() {
417+
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
418+
GenericArgKind::Type(_) => self
419+
.next_ty_var(TypeVariableOrigin {
420+
span: rustc_span::DUMMY_SP,
421+
kind: TypeVariableOriginKind::MiscVariable,
422+
})
423+
.into(),
424+
GenericArgKind::Const(arg) => self
425+
.next_const_var(
426+
arg.ty(),
427+
ConstVariableOrigin {
428+
span: rustc_span::DUMMY_SP,
429+
kind: ConstVariableOriginKind::MiscVariable,
430+
},
431+
)
432+
.into(),
433+
}
434+
}))
435+
.unwrap()
436+
.into_buffer();
437+
412438
err.span_suggestion_verbose(
413439
insert_span,
414-
&format!("consider specifying the generic argument{}", pluralize!(args.len()),),
440+
&format!(
441+
"consider specifying the generic argument{}",
442+
pluralize!(generic_args.len()),
443+
),
415444
format!("::<{}>", args),
416445
Applicability::HasPlaceholders,
417446
);

compiler/rustc_metadata/src/locator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
//!
6868
//! ## What criterion to select on?
6969
//!
70-
//! This a pretty tricky area of loading crates. Given a file, how do we know
70+
//! This is a pretty tricky area of loading crates. Given a file, how do we know
7171
//! whether it's the right crate? Currently, the rules look along these lines:
7272
//!
7373
//! 1. Does the filename match an rlib/dylib pattern? That is to say, does the

compiler/rustc_middle/src/ty/diagnostics.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::ops::ControlFlow;
44

55
use crate::ty::{
6-
visit::TypeVisitable, Const, ConstKind, DefIdTree, ExistentialPredicate, InferTy,
6+
visit::TypeVisitable, Const, ConstKind, DefIdTree, ExistentialPredicate, InferConst, InferTy,
77
PolyTraitPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
88
};
99

@@ -82,15 +82,18 @@ pub trait IsSuggestable<'tcx> {
8282
/// meaningful rendered suggestions when pretty-printed. We leave some
8383
/// nonsense, such as region vars, since those render as `'_` and are
8484
/// usually okay to reinterpret as elided lifetimes.
85-
fn is_suggestable(self, tcx: TyCtxt<'tcx>) -> bool;
85+
///
86+
/// Only if `infer_suggestable` is true, we consider type and const
87+
/// inference variables to be suggestable.
88+
fn is_suggestable(self, tcx: TyCtxt<'tcx>, infer_suggestable: bool) -> bool;
8689
}
8790

8891
impl<'tcx, T> IsSuggestable<'tcx> for T
8992
where
9093
T: TypeVisitable<'tcx>,
9194
{
92-
fn is_suggestable(self, tcx: TyCtxt<'tcx>) -> bool {
93-
self.visit_with(&mut IsSuggestableVisitor { tcx }).is_continue()
95+
fn is_suggestable(self, tcx: TyCtxt<'tcx>, infer_suggestable: bool) -> bool {
96+
self.visit_with(&mut IsSuggestableVisitor { tcx, infer_suggestable }).is_continue()
9497
}
9598
}
9699

@@ -100,7 +103,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
100103
err: &mut Diagnostic,
101104
trait_pred: PolyTraitPredicate<'tcx>,
102105
) -> bool {
103-
if !trait_pred.is_suggestable(tcx) {
106+
if !trait_pred.is_suggestable(tcx, false) {
104107
return false;
105108
}
106109

@@ -419,13 +422,16 @@ impl<'v> hir::intravisit::Visitor<'v> for StaticLifetimeVisitor<'v> {
419422

420423
pub struct IsSuggestableVisitor<'tcx> {
421424
tcx: TyCtxt<'tcx>,
425+
infer_suggestable: bool,
422426
}
423427

424428
impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> {
425429
type BreakTy = ();
426430

427431
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
428432
match t.kind() {
433+
Infer(InferTy::TyVar(_)) if self.infer_suggestable => {}
434+
429435
FnDef(..)
430436
| Closure(..)
431437
| Infer(..)
@@ -479,6 +485,8 @@ impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> {
479485

480486
fn visit_const(&mut self, c: Const<'tcx>) -> ControlFlow<Self::BreakTy> {
481487
match c.kind() {
488+
ConstKind::Infer(InferConst::Var(_)) if self.infer_suggestable => {}
489+
482490
ConstKind::Infer(..)
483491
| ConstKind::Bound(..)
484492
| ConstKind::Placeholder(..)

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
21112111
let res = match kind {
21122112
ItemRibKind(..) | AssocItemRibKind => Res::Def(def_kind, def_id.to_def_id()),
21132113
NormalRibKind => Res::Err,
2114-
_ => bug!("Unexpected rib kind {:?}", kind),
2114+
_ => span_bug!(param.ident.span, "Unexpected rib kind {:?}", kind),
21152115
};
21162116
self.r.record_partial_res(param.id, PartialRes::new(res));
21172117
rib.bindings.insert(ident, res);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ fn suggest_restriction<'tcx>(
378378
replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name))
379379
.to_ty(tcx),
380380
});
381-
if !trait_pred.is_suggestable(tcx) {
381+
if !trait_pred.is_suggestable(tcx, false) {
382382
return;
383383
}
384384
// We know we have an `impl Trait` that doesn't satisfy a required projection.
@@ -417,7 +417,7 @@ fn suggest_restriction<'tcx>(
417417
Applicability::MaybeIncorrect,
418418
);
419419
} else {
420-
if !trait_pred.is_suggestable(tcx) {
420+
if !trait_pred.is_suggestable(tcx, false) {
421421
return;
422422
}
423423
// Trivial case: `T` needs an extra bound: `T: Bound`.
@@ -586,7 +586,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
586586
// else in the predicate.
587587
if !trait_pred.skip_binder().trait_ref.substs[1..]
588588
.iter()
589-
.all(|g| g.is_suggestable(self.tcx))
589+
.all(|g| g.is_suggestable(self.tcx, false))
590590
{
591591
return;
592592
}

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8686
let param_type = tcx.infer_ctxt().enter(|infcx| {
8787
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id))
8888
});
89-
if param_type.is_suggestable(tcx) {
89+
if param_type.is_suggestable(tcx, false) {
9090
err.span_suggestion(
9191
tcx.def_span(src_def_id),
9292
"consider changing this type parameter to be a `const` generic",

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26762676
span,
26772677
ty,
26782678
opt_sugg: Some((span, Applicability::MachineApplicable))
2679-
.filter(|_| ty.is_suggestable(tcx)),
2679+
.filter(|_| ty.is_suggestable(tcx, false)),
26802680
});
26812681

26822682
ty

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10701070
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
10711071
if expected_ty.is_unit() {
10721072
"()".to_string()
1073-
} else if expected_ty.is_suggestable(tcx) {
1073+
} else if expected_ty.is_suggestable(tcx, false) {
10741074
format!("/* {} */", expected_ty)
10751075
} else {
10761076
"/* value */".to_string()

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
506506
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
507507
// Only suggest changing the return type for methods that
508508
// haven't set a return type at all (and aren't `fn main()` or an impl).
509-
match (&fn_decl.output, found.is_suggestable(self.tcx), can_suggest, expected.is_unit()) {
509+
match (
510+
&fn_decl.output,
511+
found.is_suggestable(self.tcx, false),
512+
can_suggest,
513+
expected.is_unit(),
514+
) {
510515
(&hir::FnRetTy::DefaultReturn(span), true, true, true) => {
511516
err.subdiagnostic(AddReturnTypeSuggestion::Add { span, found });
512517
true

compiler/rustc_typeck/src/collect.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
19291929
visitor.visit_ty(ty);
19301930
let mut diag = bad_placeholder(tcx, visitor.0, "return type");
19311931
let ret_ty = fn_sig.skip_binder().output();
1932-
if ret_ty.is_suggestable(tcx) {
1932+
if ret_ty.is_suggestable(tcx, false) {
19331933
diag.span_suggestion(
19341934
ty.span,
19351935
"replace with the correct return type",
@@ -1938,7 +1938,12 @@ fn infer_return_ty_for_fn_sig<'tcx>(
19381938
);
19391939
} else if matches!(ret_ty.kind(), ty::FnDef(..)) {
19401940
let fn_sig = ret_ty.fn_sig(tcx);
1941-
if fn_sig.skip_binder().inputs_and_output.iter().all(|t| t.is_suggestable(tcx)) {
1941+
if fn_sig
1942+
.skip_binder()
1943+
.inputs_and_output
1944+
.iter()
1945+
.all(|t| t.is_suggestable(tcx, false))
1946+
{
19421947
diag.span_suggestion(
19431948
ty.span,
19441949
"replace with the correct return type",

src/test/ui/closures/issue-99565.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![crate_type = "lib"]
2+
3+
fn foo<T, U>(_: U) {}
4+
5+
fn bar() {
6+
foo(|| {}); //~ ERROR type annotations needed
7+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-99565.rs:6:5
3+
|
4+
LL | foo(|| {});
5+
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
6+
|
7+
help: consider specifying the generic arguments
8+
|
9+
LL | foo::<T, _>(|| {});
10+
| ++++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

src/test/ui/issues/issue-23041.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | b.downcast_ref::<fn(_)->_>();
55
| ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the associated function `downcast_ref`
66
|
7-
help: consider specifying the generic arguments
7+
help: consider specifying the generic argument
88
|
99
LL | b.downcast_ref::<fn(_) -> _>();
1010
| ~~~~~~~~~~~~~~

src/test/ui/issues/issue-24013.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
55
| ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `swap`
66
|
7-
help: consider specifying the generic arguments
7+
help: consider specifying the generic argument
88
|
99
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
1010
| ~~~~~~~~~~

src/tools/rustbook/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ clap = "3.1.1"
99
env_logger = "0.7.1"
1010

1111
[dependencies.mdbook]
12-
version = "0.4.18"
12+
version = "0.4.21"
1313
default-features = false
1414
features = ["search"]

0 commit comments

Comments
 (0)