-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Lower all trivial const paths as ConstArgKind::Path
#135186
base: master
Are you sure you want to change the base?
Changes from all commits
39b402e
ef6597f
afda88d
53457f5
ea0691b
fafacd7
2675d49
b422934
fa9e8bf
ece9e77
030354f
38e69c6
76d3584
7cbecce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::ops::ControlFlow; | ||
|
||
use crate::ptr::P; | ||
use crate::visit::{Visitor, walk_anon_const}; | ||
use crate::{DUMMY_NODE_ID, Expr, ExprKind, Path, QSelf}; | ||
|
||
impl Expr { | ||
// FIXME: update docs | ||
/// Could this expr be either `N`, or `{ N }`, where `N` is a const parameter. | ||
/// | ||
/// If this is not the case, name resolution does not resolve `N` when using | ||
/// `min_const_generics` as more complex expressions are not supported. | ||
/// | ||
/// Does not ensure that the path resolves to a const param, the caller should check this. | ||
/// This also does not consider macros, so it's only correct after macro-expansion. | ||
pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool { | ||
let this = self.maybe_unwrap_block(); | ||
if allow_mgca_arg { | ||
MGCATrivialConstArgVisitor::new().visit_expr(this).is_continue() | ||
} else { | ||
if let ExprKind::Path(None, path) = &this.kind | ||
&& path.is_potential_trivial_const_arg(&None, allow_mgca_arg) | ||
{ | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Path { | ||
// FIXME: add docs | ||
#[tracing::instrument(level = "debug", ret)] | ||
pub fn is_potential_trivial_const_arg( | ||
&self, | ||
qself: &Option<P<QSelf>>, | ||
allow_mgca_arg: bool, | ||
) -> bool { | ||
if allow_mgca_arg { | ||
let mut visitor = MGCATrivialConstArgVisitor::new(); | ||
visitor.visit_qself(qself).is_continue() | ||
&& visitor.visit_path(self, DUMMY_NODE_ID).is_continue() | ||
} else { | ||
qself.is_none() | ||
&& self.segments.len() == 1 | ||
&& self.segments.iter().all(|seg| seg.args.is_none()) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct MGCATrivialConstArgVisitor {} | ||
|
||
impl MGCATrivialConstArgVisitor { | ||
pub(crate) fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl<'ast> Visitor<'ast> for MGCATrivialConstArgVisitor { | ||
type Result = ControlFlow<()>; | ||
|
||
fn visit_anon_const(&mut self, c: &'ast crate::AnonConst) -> Self::Result { | ||
let expr = c.value.maybe_unwrap_block(); | ||
match &expr.kind { | ||
ExprKind::Path(_, _) => {} | ||
_ => return ControlFlow::Break(()), | ||
} | ||
walk_anon_const(self, c) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ use tracing::{debug, instrument}; | |
|
||
use crate::check::intrinsic::intrinsic_operation_unsafety; | ||
use crate::errors; | ||
use crate::hir_ty_lowering::errors::assoc_kind_str; | ||
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason}; | ||
|
||
pub(crate) mod dump; | ||
|
@@ -468,14 +469,56 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> { | |
item_segment: &hir::PathSegment<'tcx>, | ||
poly_trait_ref: ty::PolyTraitRef<'tcx>, | ||
) -> Ty<'tcx> { | ||
match self.lower_assoc_shared( | ||
span, | ||
item_def_id, | ||
item_segment, | ||
poly_trait_ref, | ||
ty::AssocKind::Type, | ||
) { | ||
Ok((def_id, args)) => Ty::new_projection_from_args(self.tcx(), def_id, args), | ||
Err(witness) => Ty::new_error(self.tcx(), witness), | ||
} | ||
} | ||
|
||
fn lower_assoc_const( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would probably also be good to unify with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. These two could now be on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left a comment as a proper review comment, I think we can just delete both. If we ever need this/that level of flexibility it can be re-added but for now it seems unnecessary |
||
&self, | ||
span: Span, | ||
item_def_id: DefId, | ||
item_segment: &hir::PathSegment<'tcx>, | ||
poly_trait_ref: ty::PolyTraitRef<'tcx>, | ||
) -> Const<'tcx> { | ||
match self.lower_assoc_shared( | ||
span, | ||
item_def_id, | ||
item_segment, | ||
poly_trait_ref, | ||
ty::AssocKind::Const, | ||
) { | ||
Ok((def_id, args)) => { | ||
let uv = ty::UnevaluatedConst::new(def_id, args); | ||
Const::new_unevaluated(self.tcx(), uv) | ||
} | ||
Err(witness) => Const::new_error(self.tcx(), witness), | ||
} | ||
} | ||
|
||
fn lower_assoc_shared( | ||
&self, | ||
span: Span, | ||
item_def_id: DefId, | ||
item_segment: &rustc_hir::PathSegment<'tcx>, | ||
poly_trait_ref: ty::PolyTraitRef<'tcx>, | ||
kind: ty::AssocKind, | ||
) -> Result<(DefId, ty::GenericArgsRef<'tcx>), ErrorGuaranteed> { | ||
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() { | ||
let item_args = self.lowerer().lower_generic_args_of_assoc_item( | ||
span, | ||
item_def_id, | ||
item_segment, | ||
trait_ref.args, | ||
); | ||
Ty::new_projection_from_args(self.tcx(), item_def_id, item_args) | ||
Ok((item_def_id, item_args)) | ||
} else { | ||
// There are no late-bound regions; we can just ignore the binder. | ||
let (mut mpart_sugg, mut inferred_sugg) = (None, None); | ||
|
@@ -536,16 +579,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> { | |
} | ||
_ => {} | ||
} | ||
Ty::new_error( | ||
self.tcx(), | ||
self.tcx().dcx().emit_err(errors::AssociatedItemTraitUninferredGenericParams { | ||
span, | ||
inferred_sugg, | ||
bound, | ||
mpart_sugg, | ||
what: "type", | ||
}), | ||
) | ||
|
||
Err(self.tcx().dcx().emit_err(errors::AssociatedItemTraitUninferredGenericParams { | ||
span, | ||
inferred_sugg, | ||
bound, | ||
mpart_sugg, | ||
what: assoc_kind_str(kind), | ||
})) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a visitor is not really necessary for MGCA 🤔 The only real requirement is the at-most-one-layer-of-braces thing so really this is just a check for if the expr is a
ExprKind::Path
or a bracedExprKind::Path
.for
Path::is_potential_trivial_const_arg
i think we should just always be returningtrue
ifallow_mgca_arg
is set since we should support every path 🤔 (atleast at the ast/hir level). forExpr::is_potential_trivial_const_arg
whenallow_mgca_arg
is set its just a check forExprKind::Path