Skip to content

Commit 2d8d559

Browse files
committed
Auto merge of #68078 - Centril:rollup-qvq052k, r=Centril
Rollup of 6 pull requests Successful merges: - #66463 (Point at opaque and closure type definitions in type errors) - #67501 (Reduce special treatment for zsts) - #67820 (Parse the syntax described in RFC 2632) - #67922 (rustc_ast_lowering: misc cleanup & rustc dep reductions) - #68071 (Extend support of `_` in type parameters) - #68073 (expect `fn` after `const unsafe` / `const extern`) Failed merges: r? @ghost
2 parents 72b2bd5 + 6f3f1c5 commit 2d8d559

File tree

97 files changed

+2072
-1038
lines changed

Some content is hidden

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

97 files changed

+2072
-1038
lines changed

src/librustc/infer/canonical/query_response.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::traits::{Obligation, ObligationCause, PredicateObligation};
2222
use crate::ty::fold::TypeFoldable;
2323
use crate::ty::subst::{GenericArg, GenericArgKind};
2424
use crate::ty::{self, BoundVar, Ty, TyCtxt};
25-
use crate::util::captures::Captures;
25+
use rustc_data_structures::captures::Captures;
2626
use rustc_index::vec::Idx;
2727
use rustc_index::vec::IndexVec;
2828
use rustc_span::DUMMY_SP;

src/librustc/infer/error_reporting/mod.rs

+139-5
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ use rustc_hir as hir;
6868
use rustc_hir::def_id::DefId;
6969
use rustc_hir::Node;
7070

71-
use errors::{struct_span_err, Applicability, DiagnosticBuilder, DiagnosticStyledString};
71+
use errors::{
72+
pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticStyledString,
73+
};
74+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7275
use rustc_error_codes::*;
73-
use rustc_span::{Pos, Span};
76+
use rustc_span::{DesugaringKind, Pos, Span};
7477
use rustc_target::spec::abi;
7578
use std::{cmp, fmt};
7679

@@ -1289,6 +1292,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12891292
mut values: Option<ValuePairs<'tcx>>,
12901293
terr: &TypeError<'tcx>,
12911294
) {
1295+
let span = cause.span(self.tcx);
1296+
12921297
// For some types of errors, expected-found does not make
12931298
// sense, so just ignore the values we were given.
12941299
match terr {
@@ -1298,6 +1303,100 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12981303
_ => {}
12991304
}
13001305

1306+
struct OpaqueTypesVisitor<'tcx> {
1307+
types: FxHashMap<TyCategory, FxHashSet<Span>>,
1308+
expected: FxHashMap<TyCategory, FxHashSet<Span>>,
1309+
found: FxHashMap<TyCategory, FxHashSet<Span>>,
1310+
ignore_span: Span,
1311+
tcx: TyCtxt<'tcx>,
1312+
}
1313+
1314+
impl<'tcx> OpaqueTypesVisitor<'tcx> {
1315+
fn visit_expected_found(
1316+
tcx: TyCtxt<'tcx>,
1317+
expected: Ty<'tcx>,
1318+
found: Ty<'tcx>,
1319+
ignore_span: Span,
1320+
) -> Self {
1321+
let mut types_visitor = OpaqueTypesVisitor {
1322+
types: Default::default(),
1323+
expected: Default::default(),
1324+
found: Default::default(),
1325+
ignore_span,
1326+
tcx,
1327+
};
1328+
// The visitor puts all the relevant encountered types in `self.types`, but in
1329+
// here we want to visit two separate types with no relation to each other, so we
1330+
// move the results from `types` to `expected` or `found` as appropriate.
1331+
expected.visit_with(&mut types_visitor);
1332+
std::mem::swap(&mut types_visitor.expected, &mut types_visitor.types);
1333+
found.visit_with(&mut types_visitor);
1334+
std::mem::swap(&mut types_visitor.found, &mut types_visitor.types);
1335+
types_visitor
1336+
}
1337+
1338+
fn report(&self, err: &mut DiagnosticBuilder<'_>) {
1339+
self.add_labels_for_types(err, "expected", &self.expected);
1340+
self.add_labels_for_types(err, "found", &self.found);
1341+
}
1342+
1343+
fn add_labels_for_types(
1344+
&self,
1345+
err: &mut DiagnosticBuilder<'_>,
1346+
target: &str,
1347+
types: &FxHashMap<TyCategory, FxHashSet<Span>>,
1348+
) {
1349+
for (key, values) in types.iter() {
1350+
let count = values.len();
1351+
let kind = key.descr();
1352+
for sp in values {
1353+
err.span_label(
1354+
*sp,
1355+
format!(
1356+
"{}{}{} {}{}",
1357+
if sp.is_desugaring(DesugaringKind::Async) {
1358+
"the `Output` of this `async fn`'s "
1359+
} else if count == 1 {
1360+
"the "
1361+
} else {
1362+
""
1363+
},
1364+
if count > 1 { "one of the " } else { "" },
1365+
target,
1366+
kind,
1367+
pluralize!(count),
1368+
),
1369+
);
1370+
}
1371+
}
1372+
}
1373+
}
1374+
1375+
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1376+
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
1377+
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
1378+
let span = self.tcx.def_span(def_id);
1379+
// Avoid cluttering the output when the "found" and error span overlap:
1380+
//
1381+
// error[E0308]: mismatched types
1382+
// --> $DIR/issue-20862.rs:2:5
1383+
// |
1384+
// LL | |y| x + y
1385+
// | ^^^^^^^^^
1386+
// | |
1387+
// | the found closure
1388+
// | expected `()`, found closure
1389+
// |
1390+
// = note: expected unit type `()`
1391+
// found closure `[closure@$DIR/issue-20862.rs:2:5: 2:14 x:_]`
1392+
if !self.ignore_span.overlaps(span) {
1393+
self.types.entry(kind).or_default().insert(span);
1394+
}
1395+
}
1396+
t.super_visit_with(self)
1397+
}
1398+
}
1399+
13011400
debug!("note_type_err(diag={:?})", diag);
13021401
let (expected_found, exp_found, is_simple_error) = match values {
13031402
None => (None, None, false),
@@ -1306,6 +1405,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13061405
ValuePairs::Types(exp_found) => {
13071406
let is_simple_err =
13081407
exp_found.expected.is_simple_text() && exp_found.found.is_simple_text();
1408+
OpaqueTypesVisitor::visit_expected_found(
1409+
self.tcx,
1410+
exp_found.expected,
1411+
exp_found.found,
1412+
span,
1413+
)
1414+
.report(diag);
13091415

13101416
(is_simple_err, Some(exp_found))
13111417
}
@@ -1323,8 +1429,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13231429
}
13241430
};
13251431

1326-
let span = cause.span(self.tcx);
1327-
13281432
// Ignore msg for object safe coercion
13291433
// since E0038 message will be printed
13301434
match terr {
@@ -1336,7 +1440,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13361440
}
13371441
}
13381442
};
1339-
13401443
if let Some((expected, found)) = expected_found {
13411444
let expected_label = exp_found.map_or("type".into(), |ef| ef.expected.prefix_string());
13421445
let found_label = exp_found.map_or("type".into(), |ef| ef.found.prefix_string());
@@ -1933,3 +2036,34 @@ impl<'tcx> ObligationCause<'tcx> {
19332036
}
19342037
}
19352038
}
2039+
2040+
/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
2041+
/// extra information about each type, but we only care about the category.
2042+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2043+
crate enum TyCategory {
2044+
Closure,
2045+
Opaque,
2046+
Generator,
2047+
Foreign,
2048+
}
2049+
2050+
impl TyCategory {
2051+
fn descr(&self) -> &'static str {
2052+
match self {
2053+
Self::Closure => "closure",
2054+
Self::Opaque => "opaque type",
2055+
Self::Generator => "generator",
2056+
Self::Foreign => "foreign type",
2057+
}
2058+
}
2059+
2060+
pub fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
2061+
match ty.kind {
2062+
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2063+
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
2064+
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
2065+
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2066+
_ => None,
2067+
}
2068+
}
2069+
}

src/librustc/infer/outlives/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::infer::{GenericKind, VerifyBound};
33
use crate::traits;
44
use crate::ty::subst::{InternalSubsts, Subst};
55
use crate::ty::{self, Ty, TyCtxt};
6-
use crate::util::captures::Captures;
6+
use rustc_data_structures::captures::Captures;
77
use rustc_hir::def_id::DefId;
88

99
/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ pub mod ty;
100100

101101
pub mod util {
102102
pub mod bug;
103-
pub mod captures;
104103
pub mod common;
105104
}
106105

src/librustc/middle/cstore.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use crate::hir::map as hir_map;
66
use crate::hir::map::definitions::{DefKey, DefPathTable};
77
use crate::session::search_paths::PathKind;
8-
use crate::session::{CrateDisambiguator, Session};
9-
use crate::ty::{self, TyCtxt};
8+
use crate::session::CrateDisambiguator;
9+
use crate::ty::TyCtxt;
1010

1111
use rustc_data_structures::svh::Svh;
1212
use rustc_data_structures::sync::{self, MetadataRef};
@@ -208,7 +208,6 @@ pub trait CrateStore {
208208
fn crate_is_private_dep_untracked(&self, cnum: CrateNum) -> bool;
209209
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
210210
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
211-
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
212211

213212
// This is basically a 1-based range of ints, which is a little
214213
// silly - I may fix that.

src/librustc/traits/error_reporting.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
TraitNotObjectSafe,
77
};
88

9-
use crate::infer::error_reporting::TypeAnnotationNeeded as ErrorCode;
9+
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1010
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1111
use crate::infer::{self, InferCtxt};
1212
use crate::mir::interpret::ErrorHandled;
@@ -446,7 +446,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
446446
flags.push((sym::from_method, Some(method.to_string())));
447447
}
448448
}
449-
if let Some(t) = self.get_parent_trait_ref(&obligation.cause.code) {
449+
if let Some((t, _)) = self.get_parent_trait_ref(&obligation.cause.code) {
450450
flags.push((sym::parent_trait, Some(t)));
451451
}
452452

@@ -665,13 +665,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
665665
}
666666

667667
/// Gets the parent trait chain start
668-
fn get_parent_trait_ref(&self, code: &ObligationCauseCode<'tcx>) -> Option<String> {
668+
fn get_parent_trait_ref(
669+
&self,
670+
code: &ObligationCauseCode<'tcx>,
671+
) -> Option<(String, Option<Span>)> {
669672
match code {
670673
&ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
671674
let parent_trait_ref = self.resolve_vars_if_possible(&data.parent_trait_ref);
672675
match self.get_parent_trait_ref(&data.parent_code) {
673676
Some(t) => Some(t),
674-
None => Some(parent_trait_ref.skip_binder().self_ty().to_string()),
677+
None => {
678+
let ty = parent_trait_ref.skip_binder().self_ty();
679+
let span =
680+
TyCategory::from_ty(ty).map(|(_, def_id)| self.tcx.def_span(def_id));
681+
Some((ty.to_string(), span))
682+
}
675683
}
676684
}
677685
_ => None,
@@ -719,9 +727,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
719727
return;
720728
}
721729
let trait_ref = trait_predicate.to_poly_trait_ref();
722-
let (post_message, pre_message) = self
730+
let (post_message, pre_message, type_def) = self
723731
.get_parent_trait_ref(&obligation.cause.code)
724-
.map(|t| (format!(" in `{}`", t), format!("within `{}`, ", t)))
732+
.map(|(t, s)| {
733+
(
734+
format!(" in `{}`", t),
735+
format!("within `{}`, ", t),
736+
s.map(|s| (format!("within this `{}`", t), s)),
737+
)
738+
})
725739
.unwrap_or_default();
726740

727741
let OnUnimplementedNote { message, label, note, enclosing_scope } =
@@ -795,6 +809,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
795809
} else {
796810
err.span_label(span, explanation);
797811
}
812+
if let Some((msg, span)) = type_def {
813+
err.span_label(span, &msg);
814+
}
798815
if let Some(ref s) = note {
799816
// If it has a custom `#[rustc_on_unimplemented]` note, let's display it
800817
err.note(s.as_str());

src/librustc/traits/project.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1717
use crate::ty::fold::{TypeFoldable, TypeFolder};
1818
use crate::ty::subst::{InternalSubsts, Subst};
1919
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt};
20-
use crate::util::common::FN_OUTPUT_NAME;
2120
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
2221
use rustc_hir::def_id::DefId;
2322
use rustc_macros::HashStable;
@@ -1364,7 +1363,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
13641363
projection_ty: ty::ProjectionTy::from_ref_and_name(
13651364
tcx,
13661365
trait_ref,
1367-
Ident::with_dummy_span(FN_OUTPUT_NAME),
1366+
Ident::with_dummy_span(rustc_hir::FN_OUTPUT_NAME),
13681367
),
13691368
ty: ret_type,
13701369
});

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use crate::ty::layout::VariantIdx;
2626
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef};
2727
use crate::ty::util::{Discr, IntTypeExt};
2828
use crate::ty::walk::TypeWalker;
29-
use crate::util::captures::Captures;
3029
use arena::SyncDroplessArena;
30+
use rustc_data_structures::captures::Captures;
3131
use rustc_data_structures::fx::FxHashMap;
3232
use rustc_data_structures::fx::FxIndexMap;
3333
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

src/librustc/ty/sty.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ use crate::ty::layout::VariantIdx;
1313
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef};
1414
use crate::ty::{self, AdtDef, DefIdTree, Discr, Ty, TyCtxt, TypeFlags, TypeFoldable};
1515
use crate::ty::{List, ParamEnv, ParamEnvAnd, TyS};
16-
use crate::util::captures::Captures;
16+
use polonius_engine::Atom;
17+
use rustc_data_structures::captures::Captures;
1718
use rustc_hir as hir;
1819
use rustc_hir::def_id::DefId;
19-
20-
use polonius_engine::Atom;
2120
use rustc_index::vec::Idx;
2221
use rustc_macros::HashStable;
2322
use rustc_span::symbol::{kw, Symbol};

src/librustc/util/common.rs

-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ use rustc_data_structures::sync::Lock;
55
use std::fmt::Debug;
66
use std::time::{Duration, Instant};
77

8-
use rustc_span::symbol::{sym, Symbol};
9-
108
#[cfg(test)]
119
mod tests;
1210

13-
// The name of the associated type for `Fn` return types.
14-
pub const FN_OUTPUT_NAME: Symbol = sym::Output;
15-
1611
pub use errors::ErrorReported;
1712

1813
pub fn to_readable_str(mut val: usize) -> String {

src/librustc_ast_lowering/item.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,16 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2525
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
2626
}
2727

28-
impl<'a, 'lowering, 'hir> ItemLowerer<'a, 'lowering, 'hir> {
29-
fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
30-
where
31-
F: FnOnce(&mut Self),
32-
{
28+
impl ItemLowerer<'_, '_, '_> {
29+
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) {
3330
let old = self.lctx.is_in_trait_impl;
34-
self.lctx.is_in_trait_impl = if let &None = trait_impl_ref { false } else { true };
31+
self.lctx.is_in_trait_impl = if let &None = impl_ref { false } else { true };
3532
f(self);
3633
self.lctx.is_in_trait_impl = old;
3734
}
3835
}
3936

40-
impl<'a, 'lowering, 'hir> Visitor<'a> for ItemLowerer<'a, 'lowering, 'hir> {
37+
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4138
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
4239
let hir_id = self.lctx.lower_node_id(n);
4340

@@ -71,6 +68,12 @@ impl<'a, 'lowering, 'hir> Visitor<'a> for ItemLowerer<'a, 'lowering, 'hir> {
7168
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
7269
let this = &mut ItemLowerer { lctx: this };
7370
if let ItemKind::Impl(.., ref opt_trait_ref, _, _) = item.kind {
71+
if opt_trait_ref.as_ref().map(|tr| tr.constness.is_some()).unwrap_or(false) {
72+
this.lctx
73+
.diagnostic()
74+
.span_err(item.span, "const trait impls are not yet implemented");
75+
}
76+
7477
this.with_trait_impl_ref(opt_trait_ref, |this| visit::walk_item(this, item));
7578
} else {
7679
visit::walk_item(this, item);

0 commit comments

Comments
 (0)