Skip to content

[AutoDiff] [AST] Handle null decl in 'printDifferentiableAttrArguments'. #36954

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

Merged
merged 1 commit into from
Apr 18, 2021
Merged
Changes from all commits
Commits
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
29 changes: 18 additions & 11 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,10 @@ static std::string getDifferentiationParametersClauseString(
static void printDifferentiableAttrArguments(
const DifferentiableAttr *attr, ASTPrinter &printer,
const PrintOptions &Options, const Decl *D, bool omitWrtClause = false) {
assert(D);
// Create a temporary string for the attribute argument text.
std::string attrArgText;
llvm::raw_string_ostream stream(attrArgText);

// Get original function.
auto *original = dyn_cast<AbstractFunctionDecl>(D);
// Handle stored/computed properties and subscript methods.
if (auto *asd = dyn_cast<AbstractStorageDecl>(D))
original = asd->getAccessor(AccessorKind::Get);
assert(original && "Must resolve original declaration");

// Print comma if not leading clause.
bool isLeadingClause = false;
auto printCommaIfNecessary = [&] {
Expand Down Expand Up @@ -570,6 +562,23 @@ static void printDifferentiableAttrArguments(
llvm_unreachable("Impossible case `NonDifferentiable`");
}

// If the declaration is not available, there is not enough context to print
// the differentiability parameters or the 'where' clause, so just print the
// differentiability kind if applicable (when not `Normal`).
if (!D) {
if (attr->getDifferentiabilityKind() != DifferentiabilityKind::Normal) {
printer << '(' << stream.str() << ')';
}
return;
}

// Get original function.
auto *original = dyn_cast<AbstractFunctionDecl>(D);
// Handle stored/computed properties and subscript methods.
if (auto *asd = dyn_cast<AbstractStorageDecl>(D))
original = asd->getAccessor(AccessorKind::Get);
assert(original && "Must resolve original declaration");

// Print differentiation parameters clause, unless it is to be omitted.
if (!omitWrtClause) {
auto diffParamsString = getDifferentiationParametersClauseString(
Expand Down Expand Up @@ -616,9 +625,7 @@ static void printDifferentiableAttrArguments(
return;

// Otherwise, print the attribute argument text enclosed in parentheses.
printer << '(';
printer << stream.str();
printer << ')';
printer << '(' << stream.str() << ')';
}

void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
Expand Down