@@ -2198,6 +2198,50 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
21982198 return true ;
21992199}
22002200
2201+ static bool interp__builtin_is_within_lifetime (InterpState &S, CodePtr OpPC,
2202+ const CallExpr *Call) {
2203+
2204+ if (!S.inConstantContext ())
2205+ return false ;
2206+
2207+ const Pointer &Ptr = S.Stk .peek <Pointer>();
2208+
2209+ auto Error = [&](int Diag) {
2210+ bool CalledFromStd = false ;
2211+ const auto *Callee = S.Current ->getCallee ();
2212+ if (Callee && Callee->isInStdNamespace ()) {
2213+ const IdentifierInfo *Identifier = Callee->getIdentifier ();
2214+ CalledFromStd = Identifier && Identifier->isStr (" is_within_lifetime" );
2215+ }
2216+ S.CCEDiag (CalledFromStd
2217+ ? S.Current ->Caller ->getSource (S.Current ->getRetPC ())
2218+ : S.Current ->getSource (OpPC),
2219+ diag::err_invalid_is_within_lifetime)
2220+ << (CalledFromStd ? " std::is_within_lifetime"
2221+ : " __builtin_is_within_lifetime" )
2222+ << Diag;
2223+ return false ;
2224+ };
2225+
2226+ if (Ptr.isZero ())
2227+ return Error (0 );
2228+ if (Ptr.isOnePastEnd ())
2229+ return Error (1 );
2230+
2231+ bool Result = true ;
2232+ if (!Ptr.isActive ()) {
2233+ Result = false ;
2234+ } else {
2235+ if (!CheckLive (S, OpPC, Ptr, AK_Read))
2236+ return false ;
2237+ if (!CheckMutable (S, OpPC, Ptr))
2238+ return false ;
2239+ }
2240+
2241+ pushInteger (S, Result, Call->getType ());
2242+ return true ;
2243+ }
2244+
22012245bool InterpretBuiltin (InterpState &S, CodePtr OpPC, const CallExpr *Call,
22022246 uint32_t BuiltinID) {
22032247 if (!S.getASTContext ().BuiltinInfo .isConstantEvaluated (BuiltinID))
@@ -2707,6 +2751,11 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
27072751 return false ;
27082752 break ;
27092753
2754+ case Builtin::BI__builtin_is_within_lifetime:
2755+ if (!interp__builtin_is_within_lifetime (S, OpPC, Call))
2756+ return false ;
2757+ break ;
2758+
27102759 default :
27112760 S.FFDiag (S.Current ->getLocation (OpPC),
27122761 diag::note_invalid_subexpr_in_const_expr)
0 commit comments