Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 11 pull requests #105360

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fe2f40a
Add an example for Pin::new()
ch-iv Nov 8, 2022
8b5379e
Add example for the into_inner() method
ch-iv Nov 9, 2022
dfa90e7
fix formatting
ch-iv Nov 9, 2022
fd8ebea
Merge branch 'documentation-104107' of https://github.com/ch-iv/rust …
ch-iv Nov 9, 2022
6f48b60
Repace inaccruate comment
ch-iv Nov 9, 2022
a4ee02b
formatting
ch-iv Nov 9, 2022
06c2ce0
Update library/core/src/pin.rs
ch-iv Nov 10, 2022
0b1ead3
Merge branch 'rust-lang:master' into documentation-104107
ch-iv Nov 23, 2022
7a47e2c
Demonstrate with an &mut reference
ch-iv Nov 23, 2022
0e19fb9
While parsing enum variant, the error message always disappear
Nov 30, 2022
c823dfa
remove no-op 'let _ = '
RalfJung Dec 4, 2022
44948d1
Recurse into nested impl-trait when computing variance.
cjgillot Dec 4, 2022
19175e9
Synthesize generics for bad auto traits in dyn types
compiler-errors Dec 5, 2022
f4c76b1
Be more careful about unresolved exprs in suggestion
compiler-errors Dec 5, 2022
da929fa
Make get_impl_future_output_ty work with AFIT
compiler-errors Dec 5, 2022
c9bab74
support `Expr` in `is_const_evaluatable` and `compute`
BoxyUwU Dec 5, 2022
e940f84
drive-by: Default param for ToPredicate
compiler-errors Dec 6, 2022
d2a80c1
Avoid noting cause code (which is usually misc, b/c codegen) for opaq…
compiler-errors Dec 6, 2022
6dc2aa2
Add GenericParamDef::to_error and InternalSubsts::extend_with_error
compiler-errors Dec 6, 2022
eff7645
Avoid ICE by accounting for missing type
estebank Dec 6, 2022
e802165
On E0195 point at where clause lifetime bounds
estebank Nov 28, 2022
9ffd086
review comment: add test case
estebank Nov 28, 2022
7b2f2a8
Rollup merge of #104195 - ch-iv:documentation-104107, r=eholk
matthiaskrgr Dec 6, 2022
33bbb02
Rollup merge of #105005 - estebank:where-clause-lts, r=compiler-errors
matthiaskrgr Dec 6, 2022
fec17ca
Rollup merge of #105098 - lyming2007:issue-103869-fix, r=eholk
matthiaskrgr Dec 6, 2022
7f4afb8
Rollup merge of #105243 - RalfJung:no-op-let, r=Mark-Simulacrum
matthiaskrgr Dec 6, 2022
c915d72
Rollup merge of #105254 - cjgillot:issue-105251, r=oli-obk
matthiaskrgr Dec 6, 2022
b17fc3b
Rollup merge of #105287 - compiler-errors:issue-105275, r=eholk
matthiaskrgr Dec 6, 2022
ce3c73d
Rollup merge of #105310 - compiler-errors:issue-105288, r=eholk
matthiaskrgr Dec 6, 2022
9db3c6c
Rollup merge of #105318 - compiler-errors:issue-105304, r=jackh726
matthiaskrgr Dec 6, 2022
cd2eed4
Rollup merge of #105339 - BoxyUwU:wf_ct_kind_expr, r=TaKO8Ki
matthiaskrgr Dec 6, 2022
96888b9
Rollup merge of #105340 - estebank:ice-ice-baby, r=compiler-errors
matthiaskrgr Dec 6, 2022
1dfedd6
Rollup merge of #105342 - compiler-errors:note_cause_code-takes-predi…
matthiaskrgr Dec 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

pub(super) fn prove_predicates(
&mut self,
predicates: impl IntoIterator<
Item = impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
>,
predicates: impl IntoIterator<Item = impl ToPredicate<'tcx> + std::fmt::Debug>,
locations: Locations,
category: ConstraintCategory<'tcx>,
) {
Expand All @@ -135,7 +133,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
pub(super) fn prove_predicate(
&mut self,
predicate: impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
predicate: impl ToPredicate<'tcx> + std::fmt::Debug,
locations: Locations,
category: ConstraintCategory<'tcx>,
) {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ hir_analysis_lifetimes_or_bounds_mismatch_on_trait =
lifetime parameters or bounds on {$item_kind} `{$ident}` do not match the trait declaration
.label = lifetimes do not match {$item_kind} in trait
.generics_label = lifetimes in impl do not match this {$item_kind} in trait
.where_label = this `where` clause might not match the one in the trait
.bounds_label = this bound might be missing in the impl

hir_analysis_drop_impl_on_wrong_item =
the `Drop` trait may only be implemented for local structs, enums, and unions
Expand Down
51 changes: 40 additions & 11 deletions compiler/rustc_hir_analysis/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,27 +751,56 @@ fn check_region_bounds_on_impl_item<'tcx>(
.get_generics(impl_m.def_id.expect_local())
.expect("expected impl item to have generics or else we can't compare them")
.span;
let generics_span = if let Some(local_def_id) = trait_m.def_id.as_local() {
Some(
tcx.hir()
.get_generics(local_def_id)
.expect("expected trait item to have generics or else we can't compare them")
.span,
)
} else {
None
};

let mut generics_span = None;
let mut bounds_span = vec![];
let mut where_span = None;
if let Some(trait_node) = tcx.hir().get_if_local(trait_m.def_id)
&& let Some(trait_generics) = trait_node.generics()
{
generics_span = Some(trait_generics.span);
// FIXME: we could potentially look at the impl's bounds to not point at bounds that
// *are* present in the impl.
for p in trait_generics.predicates {
if let hir::WherePredicate::BoundPredicate(pred) = p {
for b in pred.bounds {
if let hir::GenericBound::Outlives(lt) = b {
bounds_span.push(lt.ident.span);
}
}
}
}
if let Some(impl_node) = tcx.hir().get_if_local(impl_m.def_id)
&& let Some(impl_generics) = impl_node.generics()
{
let mut impl_bounds = 0;
for p in impl_generics.predicates {
if let hir::WherePredicate::BoundPredicate(pred) = p {
for b in pred.bounds {
if let hir::GenericBound::Outlives(_) = b {
impl_bounds += 1;
}
}
}
}
if impl_bounds == bounds_span.len() {
bounds_span = vec![];
} else if impl_generics.has_where_clause_predicates {
where_span = Some(impl_generics.where_clause_span);
}
}
}
let reported = tcx
.sess
.create_err(LifetimesOrBoundsMismatchOnTrait {
span,
item_kind: assoc_item_kind_str(impl_m),
ident: impl_m.ident(tcx),
generics_span,
bounds_span,
where_span,
})
.emit_unless(delay);

return Err(reported);
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub struct LifetimesOrBoundsMismatchOnTrait {
pub span: Span,
#[label(generics_label)]
pub generics_span: Option<Span>,
#[label(where_label)]
pub where_span: Option<Span>,
#[label(bounds_label)]
pub bounds_span: Vec<Span>,
pub item_kind: &'static str,
pub ident: Ident,
}
Expand Down
42 changes: 38 additions & 4 deletions compiler/rustc_hir_analysis/src/variance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use rustc_arena::DroplessArena;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, CrateVariancesMap, TyCtxt, TypeSuperVisitable, TypeVisitable};
use rustc_middle::ty::{self, CrateVariancesMap, SubstsRef, Ty, TyCtxt};
use rustc_middle::ty::{DefIdTree, TypeSuperVisitable, TypeVisitable};
use std::ops::ControlFlow;

/// Defines the `TermsContext` basically houses an arena where we can
Expand Down Expand Up @@ -75,18 +76,50 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
// type Foo<'a, 'b, 'c> = impl Trait<'a> + 'b;
// ```
// we may not use `'c` in the hidden type.
struct OpaqueTypeLifetimeCollector {
struct OpaqueTypeLifetimeCollector<'tcx> {
tcx: TyCtxt<'tcx>,
root_def_id: DefId,
variances: Vec<ty::Variance>,
}

impl<'tcx> ty::TypeVisitor<'tcx> for OpaqueTypeLifetimeCollector {
impl<'tcx> OpaqueTypeLifetimeCollector<'tcx> {
#[instrument(level = "trace", skip(self), ret)]
fn visit_opaque(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> ControlFlow<!> {
if def_id != self.root_def_id && self.tcx.is_descendant_of(def_id, self.root_def_id) {
let child_variances = self.tcx.variances_of(def_id);
for (a, v) in substs.iter().zip(child_variances) {
if *v != ty::Bivariant {
a.visit_with(self)?;
}
}
ControlFlow::CONTINUE
} else {
substs.visit_with(self)
}
}
}

impl<'tcx> ty::TypeVisitor<'tcx> for OpaqueTypeLifetimeCollector<'tcx> {
#[instrument(level = "trace", skip(self), ret)]
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
if let ty::RegionKind::ReEarlyBound(ebr) = r.kind() {
self.variances[ebr.index as usize] = ty::Invariant;
}
r.super_visit_with(self)
}

#[instrument(level = "trace", skip(self), ret)]
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
match t.kind() {
ty::Opaque(def_id, substs) => self.visit_opaque(*def_id, substs),
ty::Projection(proj)
if self.tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder =>
{
self.visit_opaque(proj.item_def_id, proj.substs)
}
_ => t.super_visit_with(self),
}
}
}

// By default, RPIT are invariant wrt type and const generics, but they are bivariant wrt
Expand All @@ -111,7 +144,8 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
}
}

let mut collector = OpaqueTypeLifetimeCollector { variances };
let mut collector =
OpaqueTypeLifetimeCollector { tcx, root_def_id: item_def_id.to_def_id(), variances };
let id_substs = ty::InternalSubsts::identity_for_item(tcx, item_def_id.to_def_id());
for pred in tcx.bound_explicit_item_bounds(item_def_id.to_def_id()).transpose_iter() {
let pred = pred.map_bound(|(pred, _)| *pred).subst(tcx, id_substs);
Expand Down
23 changes: 13 additions & 10 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,25 +1482,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ident_name: Symbol,
}

// FIXME: This really should be taking scoping, etc into account.
impl<'v> Visitor<'v> for LetVisitor<'v> {
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
if let hir::StmtKind::Local(hir::Local { pat, init, .. }) = &ex.kind {
if let Binding(_, _, ident, ..) = pat.kind &&
ident.name == self.ident_name {
self.result = *init;
}
if let hir::StmtKind::Local(hir::Local { pat, init, .. }) = &ex.kind
&& let Binding(_, _, ident, ..) = pat.kind
&& ident.name == self.ident_name
{
self.result = *init;
} else {
hir::intravisit::walk_stmt(self, ex);
}
hir::intravisit::walk_stmt(self, ex);
}
}

let mut visitor = LetVisitor { result: None, ident_name: seg1.ident.name };
visitor.visit_body(&body);

let parent = self.tcx.hir().get_parent_node(seg1.hir_id);
if let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent) &&
let Some(expr) = visitor.result {
let self_ty = self.node_ty(expr.hir_id);
if let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent)
&& let Some(expr) = visitor.result
&& let Some(self_ty) = self.node_ty_opt(expr.hir_id)
{
let probe = self.lookup_probe(
seg2.ident,
self_ty,
Expand All @@ -1513,7 +1516,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':').unwrap(),
"you may have meant to call an instance method",
".".to_string(),
Applicability::MaybeIncorrect
Applicability::MaybeIncorrect,
);
}
}
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,15 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(

impl<'tcx> InferCtxt<'tcx> {
pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
let ty::Opaque(def_id, substs) = *ty.kind() else { return None; };
let (def_id, substs) = match *ty.kind() {
ty::Opaque(def_id, substs) => (def_id, substs),
ty::Projection(data)
if self.tcx.def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder =>
{
(data.item_def_id, data.substs)
}
_ => return None,
};

let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ impl GenericParamDef {
_ => None,
}
}

pub fn to_error<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
preceding_substs: &[ty::GenericArg<'tcx>],
) -> ty::GenericArg<'tcx> {
match &self.kind {
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
ty::GenericParamDefKind::Type { .. } => tcx.ty_error().into(),
ty::GenericParamDefKind::Const { .. } => {
tcx.const_error(tcx.bound_type_of(self.def_id).subst(tcx, preceding_substs)).into()
}
}
}
}

#[derive(Default)]
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,8 @@ impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
}
}

pub trait ToPredicate<'tcx, Predicate> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate;
pub trait ToPredicate<'tcx, P = Predicate<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> P;
}

impl<'tcx, T> ToPredicate<'tcx, T> for T {
Expand All @@ -1160,21 +1160,21 @@ impl<'tcx, T> ToPredicate<'tcx, T> for T {
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, PredicateKind<'tcx>> {
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, PredicateKind<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
tcx.mk_predicate(self)
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Clause<'tcx> {
impl<'tcx> ToPredicate<'tcx> for Clause<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause(self)))
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
let pred: PolyTraitPredicate<'tcx> = self.to_predicate(tcx);
Expand All @@ -1193,25 +1193,25 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTraitPredicate<'tcx> {
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(Clause::Trait(p))).to_predicate(tcx)
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyRegionOutlivesPredicate<'tcx> {
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(Clause::RegionOutlives(p))).to_predicate(tcx)
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTypeOutlivesPredicate<'tcx> {
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(Clause::TypeOutlives(p))).to_predicate(tcx)
}
}

impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyProjectionPredicate<'tcx> {
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(Clause::Projection(p))).to_predicate(tcx)
}
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,17 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
}
ExistentialPredicate::AutoTrait(did) => {
let trait_ref = self.rebind(tcx.mk_trait_ref(did, [self_ty]));
trait_ref.without_const().to_predicate(tcx)
let generics = tcx.generics_of(did);
let trait_ref = if generics.params.len() == 1 {
tcx.mk_trait_ref(did, [self_ty])
} else {
// If this is an ill-formed auto trait, then synthesize
// new error substs for the missing generics.
let err_substs =
ty::InternalSubsts::extend_with_error(tcx, did, &[self_ty.into()]);
tcx.mk_trait_ref(did, err_substs)
};
self.rebind(trait_ref).without_const().to_predicate(tcx)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ impl<'tcx> InternalSubsts<'tcx> {
}
}

// Extend an `original_substs` list to the full number of substs expected by `def_id`,
// filling in the missing parameters with error ty/ct or 'static regions.
pub fn extend_with_error(
tcx: TyCtxt<'tcx>,
def_id: DefId,
original_substs: &[GenericArg<'tcx>],
) -> SubstsRef<'tcx> {
ty::InternalSubsts::for_item(tcx, def_id, |def, substs| {
if let Some(subst) = original_substs.get(def.index as usize) {
*subst
} else {
def.to_error(tcx, substs)
}
})
}

#[inline]
pub fn types(&'tcx self) -> impl DoubleEndedIterator<Item = Ty<'tcx>> + 'tcx {
self.iter()
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,10 @@ impl<'a> Parser<'a> {

Ok((Some(vr), TrailingToken::MaybeComma))
},
)
).map_err(|mut err|{
err.help("enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`");
err
})
}

/// Parses `struct Foo { ... }`.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ impl<'a> Parser<'a> {
Err(e) => {
// Parsing failed, therefore it must be something more serious
// than just a missing separator.
for xx in &e.children {
// propagate the help message from sub error 'e' to main error 'expect_err;
expect_err.children.push(xx.clone());
}
expect_err.emit();

e.cancel();
Expand Down
Loading