Skip to content

Rollup of 7 pull requests #120088

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

Closed
wants to merge 17 commits into from
Closed
Changes from all commits
Commits
Show all changes
17 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
12 changes: 2 additions & 10 deletions compiler/rustc_ast/src/util/literal.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ use rustc_lexer::unescape::{
};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use std::ops::Range;
use std::{ascii, fmt, str};

// Escapes a string, represented as a symbol. Reuses the original symbol,
@@ -39,7 +38,6 @@ pub enum LitError {
InvalidFloatSuffix,
NonDecimalFloat(u32),
IntTooLarge(u32),
NulInCStr(Range<usize>),
}

impl LitKind {
@@ -156,10 +154,7 @@ impl LitKind {
let s = symbol.as_str();
let mut buf = Vec::with_capacity(s.len());
let mut error = Ok(());
unescape_c_string(s, Mode::CStr, &mut |span, c| match c {
Ok(CStrUnit::Byte(0) | CStrUnit::Char('\0')) => {
error = Err(LitError::NulInCStr(span));
}
unescape_c_string(s, Mode::CStr, &mut |_span, c| match c {
Ok(CStrUnit::Byte(b)) => buf.push(b),
Ok(CStrUnit::Char(c)) => {
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
@@ -179,10 +174,7 @@ impl LitKind {
// can convert the symbol directly to a `Lrc<u8>` on success.
let s = symbol.as_str();
let mut error = Ok(());
unescape_c_string(s, Mode::RawCStr, &mut |span, c| match c {
Ok(CStrUnit::Byte(0) | CStrUnit::Char('\0')) => {
error = Err(LitError::NulInCStr(span));
}
unescape_c_string(s, Mode::RawCStr, &mut |_, c| match c {
Ok(_) => {}
Err(err) => {
if err.is_fatal() {
14 changes: 10 additions & 4 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
@@ -754,16 +754,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
get_future_output(obligation.predicate, obligation.cause.span)
})?
}
ty::Alias(ty::Projection, _) => {
return Some(Ty::new_error_with_message(
self.tcx,
closure_span,
"this projection should have been projected to an opaque type",
));
}
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => self
.tcx
.explicit_item_bounds(def_id)
.iter_instantiated_copied(self.tcx, args)
.find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
ty::Error(_) => return Some(ret_ty),
_ => span_bug!(
closure_span,
"async fn coroutine return type not an inference variable: {ret_ty}"
),
_ => {
span_bug!(closure_span, "invalid async fn coroutine return type: {ret_ty:?}")
}
};

let output_ty = self.normalize(closure_span, output_ty);
33 changes: 23 additions & 10 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,9 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
use rustc_middle::hir::nested_filter;
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::infer::unify_key::{
ConstVariableOrigin, ConstVariableOriginKind, ConstVariableValue,
};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
use rustc_middle::ty::{self, InferConst};
@@ -178,17 +180,23 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
}
};
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
let const_getter = move |ct_vid| {
if infcx.probe_const_var(ct_vid).is_ok() {
let const_getter = move |ct_vid| match infcx
.inner
.borrow_mut()
.const_unification_table()
.probe_value(ct_vid)
{
ConstVariableValue::Known { value: _ } => {
warn!("resolved const var in error message");
}
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) =
infcx.inner.borrow_mut().const_unification_table().probe_value(ct_vid).origin.kind
{
return Some(name);
} else {
None
}
ConstVariableValue::Unknown { origin, universe: _ } => {
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = origin.kind {
return Some(name);
} else {
None
}
}
};
printer.const_infer_name_resolver = Some(Box::new(const_getter));
printer
@@ -303,7 +311,12 @@ impl<'tcx> InferCtxt<'tcx> {
GenericArgKind::Const(ct) => {
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
let origin =
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
ConstVariableValue::Known { value } => {
bug!("resolved infer var: {vid:?} {value}")
}
ConstVariableValue::Unknown { origin, universe: _ } => origin,
};
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
origin.kind
{
10 changes: 2 additions & 8 deletions compiler/rustc_infer/src/infer/freshen.rs
Original file line number Diff line number Diff line change
@@ -146,14 +146,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
let opt_ct = self
.infcx
.inner
.borrow_mut()
.const_unification_table()
.probe_value(v)
.val
.known();
let opt_ct =
self.infcx.inner.borrow_mut().const_unification_table().probe_value(v).known();
self.freshen_const(opt_ct, ty::InferConst::Var(v), ty::InferConst::Fresh, ct.ty())
}
ty::ConstKind::Infer(ty::InferConst::EffectVar(v)) => {
11 changes: 9 additions & 2 deletions compiler/rustc_infer/src/infer/fudge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_middle::infer::unify_key::ConstVidKey;
use rustc_middle::infer::unify_key::{ConstVariableOriginKind, ConstVariableValue, ConstVidKey};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};

@@ -28,10 +28,17 @@ fn const_vars_since_snapshot<'tcx>(
snapshot_var_len: usize,
) -> (Range<ConstVid>, Vec<ConstVariableOrigin>) {
let range = vars_since_snapshot(table, snapshot_var_len);

(
range.start.vid..range.end.vid,
(range.start.index()..range.end.index())
.map(|index| table.probe_value(ConstVid::from_u32(index)).origin)
.map(|index| match table.probe_value(ConstVid::from_u32(index)) {
ConstVariableValue::Known { value: _ } => ConstVariableOrigin {
kind: ConstVariableOriginKind::MiscVariable,
span: rustc_span::DUMMY_SP,
},
ConstVariableValue::Unknown { origin, universe: _ } => origin,
})
.collect(),
)
}
21 changes: 7 additions & 14 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@ use rustc_data_structures::unify as ut;
use rustc_errors::{DiagCtxt, DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
use rustc_middle::mir::ConstraintCategory;
use rustc_middle::traits::{select, DefiningAnchor};
@@ -1086,7 +1086,7 @@ impl<'tcx> InferCtxt<'tcx> {
.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } })
.new_key(ConstVariableValue::Unknown { origin, universe })
.vid;
ty::Const::new_var(self.tcx, vid, ty)
}
@@ -1095,10 +1095,7 @@ impl<'tcx> InferCtxt<'tcx> {
self.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue {
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
})
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid
}

@@ -1217,10 +1214,7 @@ impl<'tcx> InferCtxt<'tcx> {
.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue {
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
})
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid;
ty::Const::new_var(
self.tcx,
@@ -1410,9 +1404,9 @@ impl<'tcx> InferCtxt<'tcx> {
}

pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
ConstVariableValue::Known { value } => Ok(value),
ConstVariableValue::Unknown { universe } => Err(universe),
ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
}
}

@@ -1709,7 +1703,7 @@ impl<'tcx> InferCtxt<'tcx> {
// `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
//
// Not `inlined_probe_value(v)` because this call site is colder.
match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
match self.inner.borrow_mut().const_unification_table().probe_value(v) {
ConstVariableValue::Unknown { .. } => false,
ConstVariableValue::Known { .. } => true,
}
@@ -1876,7 +1870,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
.borrow_mut()
.const_unification_table()
.probe_value(vid)
.val
.known()
.unwrap_or(ct),
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self
26 changes: 11 additions & 15 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
@@ -30,14 +30,12 @@ use super::sub::Sub;
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
use crate::traits::{Obligation, PredicateObligations};
use rustc_middle::infer::canonical::OriginalQueryValues;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{AliasRelationDirection, TyVar};
use rustc_middle::ty::{IntType, UintType};
use rustc_span::DUMMY_SP;

#[derive(Clone)]
pub struct CombineFields<'infcx, 'tcx> {
@@ -328,8 +326,12 @@ impl<'tcx> InferCtxt<'tcx> {
ct: ty::Const<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> RelateResult<'tcx, ty::Const<'tcx>> {
let span =
self.inner.borrow_mut().const_unification_table().probe_value(target_vid).origin.span;
let span = match self.inner.borrow_mut().const_unification_table().probe_value(target_vid) {
ConstVariableValue::Known { value } => {
bug!("instantiating a known const var: {target_vid:?} {value} {ct}")
}
ConstVariableValue::Unknown { origin, universe: _ } => origin.span,
};
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
// constants and generic expressions are not yet handled correctly.
let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize(
@@ -340,16 +342,10 @@ impl<'tcx> InferCtxt<'tcx> {
ty::Variance::Invariant,
)?;

self.inner.borrow_mut().const_unification_table().union_value(
target_vid,
ConstVarValue {
origin: ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstInference,
span: DUMMY_SP,
},
val: ConstVariableValue::Known { value },
},
);
self.inner
.borrow_mut()
.const_unification_table()
.union_value(target_vid, ConstVariableValue::Known { value });
Ok(value)
}

15 changes: 6 additions & 9 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::mem;
use rustc_data_structures::sso::SsoHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::DefId;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
use rustc_middle::infer::unify_key::ConstVariableValue;
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
use rustc_middle::ty::visit::MaxUniverse;
@@ -431,22 +431,19 @@ where

let mut inner = self.infcx.inner.borrow_mut();
let variable_table = &mut inner.const_unification_table();
let var_value = variable_table.probe_value(vid);
match var_value.val {
match variable_table.probe_value(vid) {
ConstVariableValue::Known { value: u } => {
drop(inner);
self.relate(u, u)
}
ConstVariableValue::Unknown { universe } => {
ConstVariableValue::Unknown { origin, universe } => {
if self.for_universe.can_name(universe) {
Ok(c)
} else {
let new_var_id = variable_table
.new_key(ConstVarValue {
origin: var_value.origin,
val: ConstVariableValue::Unknown {
universe: self.for_universe,
},
.new_key(ConstVariableValue::Unknown {
origin,
universe: self.for_universe,
})
.vid;
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))
Loading