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 8 pull requests #99450

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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: 3 additions & 3 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The entry point of the NLL borrow checker.

use rustc_data_structures::vec_map::VecMap;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::InferCtxt;
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
Expand Down Expand Up @@ -44,7 +44,7 @@ pub type PoloniusOutput = Output<RustcFacts>;
/// closure requirements to propagate, and any generated errors.
pub(crate) struct NllOutput<'tcx> {
pub regioncx: RegionInferenceContext<'tcx>,
pub opaque_type_values: VecMap<DefId, OpaqueHiddenType<'tcx>>,
pub opaque_type_values: VecMap<LocalDefId, OpaqueHiddenType<'tcx>>,
pub polonius_input: Option<Box<AllFacts>>,
pub polonius_output: Option<Rc<PoloniusOutput>>,
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
Expand Down Expand Up @@ -373,7 +373,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
opaque_type_values: &VecMap<DefId, OpaqueHiddenType<'tcx>>,
opaque_type_values: &VecMap<LocalDefId, OpaqueHiddenType<'tcx>>,
errors: &mut crate::error::BorrowckErrors<'tcx>,
) {
let tcx = infcx.tcx;
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::vec_map::VecMap;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::OpaqueTyOrigin;
use rustc_infer::infer::error_reporting::unexpected_hidden_region_diagnostic;
use rustc_infer::infer::InferCtxt;
Expand Down Expand Up @@ -63,8 +63,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
&self,
infcx: &InferCtxt<'_, 'tcx>,
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
) -> VecMap<DefId, OpaqueHiddenType<'tcx>> {
let mut result: VecMap<DefId, OpaqueHiddenType<'tcx>> = VecMap::new();
) -> VecMap<LocalDefId, OpaqueHiddenType<'tcx>> {
let mut result: VecMap<LocalDefId, OpaqueHiddenType<'tcx>> = VecMap::new();
for (opaque_type_key, (concrete_type, origin)) in opaque_ty_decls {
let substs = opaque_type_key.substs;
debug!(?concrete_type, ?substs);
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// lifetimes with 'static and remapping only those used in the
// `impl Trait` return type, resulting in the parameters
// shifting.
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id);
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
debug!(?id_substs);
let map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>> =
substs.iter().enumerate().map(|(index, subst)| (subst, id_substs[index])).collect();
Expand Down Expand Up @@ -268,7 +268,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// This logic duplicates most of `check_opaque_meets_bounds`.
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
let param_env = self.tcx.param_env(def_id);
let body_id = self.tcx.local_def_id_to_hir_id(def_id.as_local().unwrap());
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
self.tcx.infer_ctxt().enter(move |infcx| {
// Require the hidden type to be well-formed with only the generics of the opaque type.
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
Expand Down Expand Up @@ -296,7 +296,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
infcx
.report_mismatched_types(
&ObligationCause::misc(instantiated_ty.span, body_id),
self.tcx.mk_opaque(def_id, id_substs),
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
definition_ty,
err,
)
Expand Down Expand Up @@ -423,7 +423,7 @@ fn check_opaque_type_parameter_valid(
struct ReverseMapper<'tcx> {
tcx: TyCtxt<'tcx>,

opaque_type_def_id: DefId,
opaque_type_def_id: LocalDefId,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
map_missing_regions_to_empty: bool,

Expand All @@ -437,7 +437,7 @@ struct ReverseMapper<'tcx> {
impl<'tcx> ReverseMapper<'tcx> {
fn new(
tcx: TyCtxt<'tcx>,
opaque_type_def_id: DefId,
opaque_type_def_id: LocalDefId,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
hidden_ty: Ty<'tcx>,
span: Span,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ E0785: include_str!("./error_codes/E0785.md"),
E0786: include_str!("./error_codes/E0786.md"),
E0787: include_str!("./error_codes/E0787.md"),
E0788: include_str!("./error_codes/E0788.md"),
E0789: include_str!("./error_codes/E0789.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard
Expand Down
45 changes: 12 additions & 33 deletions compiler/rustc_error_codes/src/error_codes/E0283.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,27 @@ An implementation cannot be chosen unambiguously because of lack of information.
Erroneous code example:

```compile_fail,E0283
trait Generator {
fn create() -> u32;
}

struct Impl;

impl Generator for Impl {
fn create() -> u32 { 1 }
}

struct AnotherImpl;
struct Foo;

impl Generator for AnotherImpl {
fn create() -> u32 { 2 }
impl Into<u32> for Foo {
fn into(self) -> u32 { 1 }
}

fn main() {
let cont: u32 = Generator::create();
// error, impossible to choose one of Generator trait implementation
// Should it be Impl or AnotherImpl, maybe something else?
}
let foo = Foo;
let bar: u32 = foo.into() * 1u32;
```

This error can be solved by adding type annotations that provide the missing
information to the compiler. In this case, the solution is to use a concrete
type:
information to the compiler. In this case, the solution is to specify the
trait's type parameter:

```
trait Generator {
fn create() -> u32;
}

struct AnotherImpl;
struct Foo;

impl Generator for AnotherImpl {
fn create() -> u32 { 2 }
impl Into<u32> for Foo {
fn into(self) -> u32 { 1 }
}

fn main() {
let gen1 = AnotherImpl::create();

// if there are multiple methods with same name (different traits)
let gen2 = <AnotherImpl as Generator>::create();
}
let foo = Foo;
let bar: u32 = Into::<u32>::into(foo) * 1u32;
```
47 changes: 47 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0789.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
You need to specify a specific implementation of the trait in order to call the
method.

Erroneous code example:

```compile_fail,E0789
trait Generator {
fn create() -> u32;
}

struct Impl;

impl Generator for Impl {
fn create() -> u32 { 1 }
}

struct AnotherImpl;

impl Generator for AnotherImpl {
fn create() -> u32 { 2 }
}

let cont: u32 = Generator::create();
// error, impossible to choose one of Generator trait implementation
// Should it be Impl or AnotherImpl, maybe something else?
```

This error can be solved by adding type annotations that provide the missing
information to the compiler. In this case, the solution is to use a concrete
type:

```
trait Generator {
fn create() -> u32;
}

struct AnotherImpl;

impl Generator for AnotherImpl {
fn create() -> u32 { 2 }
}

let gen1 = AnotherImpl::create();

// if there are multiple methods with same name (different traits)
let gen2 = <AnotherImpl as Generator>::create();
```
5 changes: 0 additions & 5 deletions compiler/rustc_error_messages/locales/en-US/lint.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ lint-atomic-ordering-invalid = `{$method}`'s failure ordering may not be `Releas
.label = invalid failure ordering
.help = consider using `Acquire` or `Relaxed` failure ordering instead

lint-atomic-ordering-invalid-fail-success = `{$method}`'s success ordering must be at least as strong as its failure ordering
.fail-label = `{$fail_ordering}` failure ordering
.success-label = `{$success_ordering}` success ordering
.suggestion = consider using `{$success_suggestion}` success ordering instead

lint-unused-op = unused {$op} that must be used
.label = the {$op} produces a value
.suggestion = use `let _ = ...` to ignore the resulting value
Expand Down
24 changes: 18 additions & 6 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ pub trait Emitter {
) {
// Check for spans in macros, before `fix_multispans_in_extern_macros`
// has a chance to replace them.
let has_macro_spans = iter::once(&*span)
let has_macro_spans: Vec<_> = iter::once(&*span)
.chain(children.iter().map(|child| &child.span))
.flat_map(|span| span.primary_spans())
.flat_map(|sp| sp.macro_backtrace())
.find_map(|expn_data| {
.filter_map(|expn_data| {
match expn_data.kind {
ExpnKind::Root => None,

Expand All @@ -413,7 +413,8 @@ pub trait Emitter {

ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
}
});
})
.collect();

if !backtrace {
self.fix_multispans_in_extern_macros(source_map, span, children);
Expand All @@ -422,11 +423,22 @@ pub trait Emitter {
self.render_multispans_macro_backtrace(span, children, backtrace);

if !backtrace {
if let Some((macro_kind, name)) = has_macro_spans {
let descr = macro_kind.descr();
if let Some((macro_kind, name)) = has_macro_spans.first() {
// Mark the actual macro this originates from
let and_then = if let Some((macro_kind, last_name)) = has_macro_spans.last()
&& last_name != name
{
let descr = macro_kind.descr();
format!(
" which comes from the expansion of the {descr} `{last_name}`",
)
} else {
"".to_string()
};

let descr = macro_kind.descr();
let msg = format!(
"this {level} originates in the {descr} `{name}` \
"this {level} originates in the {descr} `{name}`{and_then} \
(in Nightly builds, run with -Z macro-backtrace for more info)",
);

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(drain_filter)]
#![feature(backtrace)]
#![feature(if_let_guard)]
#![cfg_attr(bootstrap, feature(let_chains))]
#![feature(let_else)]
#![feature(never_type)]
#![feature(adt_const_params)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
.opaque_type_storage
.take_opaque_types()
.into_iter()
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id, k.substs), v.hidden_type.ty))
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id.to_def_id(), k.substs), v.hidden_type.ty))
.collect()
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
pub fn member_constraint(
&self,
opaque_type_def_id: DefId,
opaque_type_def_id: LocalDefId,
definition_span: Span,
hidden_ty: Ty<'tcx>,
region: ty::Region<'tcx>,
Expand Down
26 changes: 16 additions & 10 deletions compiler/rustc_infer/src/infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
return InferOk { value: ty, obligations: vec![] };
}
let mut obligations = vec![];
let replace_opaque_type = |def_id| self.opaque_type_origin(def_id, span).is_some();
let replace_opaque_type = |def_id: DefId| {
def_id
.as_local()
.map_or(false, |def_id| self.opaque_type_origin(def_id, span).is_some())
};
let value = ty.fold_with(&mut ty::fold::BottomUpFolder {
tcx: self.tcx,
lt_op: |lt| lt,
Expand Down Expand Up @@ -96,6 +100,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let (a, b) = if a_is_expected { (a, b) } else { (b, a) };
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
ty::Opaque(def_id, substs) if def_id.is_local() => {
let def_id = def_id.expect_local();
let origin = if self.defining_use_anchor.is_some() {
// Check that this is `impl Trait` type is
// declared by `parent_def_id` -- i.e., one whose
Expand Down Expand Up @@ -141,7 +146,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// no one encounters it in practice.
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
// where it is of no concern, so we only check for TAITs.
if let Some(OpaqueTyOrigin::TyAlias) = self.opaque_type_origin(did2, cause.span)
if let Some(OpaqueTyOrigin::TyAlias) =
did2.as_local().and_then(|did2| self.opaque_type_origin(did2, cause.span))
{
self.tcx
.sess
Expand Down Expand Up @@ -399,8 +405,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}

#[instrument(skip(self), level = "trace")]
pub fn opaque_type_origin(&self, opaque_def_id: DefId, span: Span) -> Option<OpaqueTyOrigin> {
let def_id = opaque_def_id.as_local()?;
pub fn opaque_type_origin(&self, def_id: LocalDefId, span: Span) -> Option<OpaqueTyOrigin> {
let opaque_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let parent_def_id = self.defining_use_anchor?;
let item_kind = &self.tcx.hir().expect_item(def_id).kind;
Expand All @@ -409,7 +414,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
span_bug!(
span,
"weird opaque type: {:#?}, {:#?}",
opaque_def_id,
def_id,
item_kind
)
};
Expand All @@ -428,12 +433,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}

#[instrument(skip(self), level = "trace")]
fn opaque_ty_origin_unchecked(&self, opaque_def_id: DefId, span: Span) -> OpaqueTyOrigin {
let def_id = opaque_def_id.as_local().unwrap();
fn opaque_ty_origin_unchecked(&self, def_id: LocalDefId, span: Span) -> OpaqueTyOrigin {
let origin = match self.tcx.hir().expect_item(def_id).kind {
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => origin,
ref itemkind => {
span_bug!(span, "weird opaque type: {:?}, {:#?}", opaque_def_id, itemkind)
span_bug!(span, "weird opaque type: {:?}, {:#?}", def_id, itemkind)
}
};
trace!(?origin);
Expand Down Expand Up @@ -557,7 +561,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
obligations = self.at(&cause, param_env).eq(prev, hidden_ty)?.obligations;
}

let item_bounds = tcx.bound_explicit_item_bounds(def_id);
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());

for predicate in item_bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
debug!(?predicate);
Expand All @@ -579,7 +583,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
// Replace all other mentions of the same opaque type with the hidden type,
// as the bounds must hold on the hidden type after all.
ty::Opaque(def_id2, substs2) if def_id == def_id2 && substs == substs2 => {
ty::Opaque(def_id2, substs2)
if def_id.to_def_id() == def_id2 && substs == substs2 =>
{
hidden_ty
}
_ => ty,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::UndoLogs;
use rustc_data_structures::unify as ut;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_index::vec::IndexVec;
use rustc_middle::infer::unify_key::{RegionVidKey, UnifiedRegion};
use rustc_middle::ty::ReStatic;
Expand Down Expand Up @@ -533,7 +533,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {

pub fn member_constraint(
&mut self,
opaque_type_def_id: DefId,
opaque_type_def_id: LocalDefId,
definition_span: Span,
hidden_ty: Ty<'tcx>,
member_region: ty::Region<'tcx>,
Expand Down
Loading