Program: 132547
Project: 132547
Buggy source:
private void defineBuiltinFunction(Context context,SwiftAST tree) throws UserException {
  final int REQUIRED_CHILDREN=5;
  assert(tree.getChildCount() >= REQUIRED_CHILDREN);
  String function=tree.child(0).getText();
  SwiftAST typeParamsT=tree.child(1);
  SwiftAST outputs=tree.child(2);
  SwiftAST inputs=tree.child(3);
  SwiftAST tclPackage=tree.child(4);
  assert(inputs.getType() == ExMParser.FORMAL_ARGUMENT_LIST);
  assert(outputs.getType() == ExMParser.FORMAL_ARGUMENT_LIST);
  assert(tclPackage.getType() == ExMParser.TCL_PACKAGE);
  assert(tclPackage.getChildCount() == 2);
  Set<String> typeParams=extractTypeParams(typeParamsT);
  FunctionDecl fdecl=FunctionDecl.fromAST(context,varCreator,exprWalker,function,inputs,outputs,typeParams);
  FunctionType ft=fdecl.getFunctionType();
  LogHelper.debug(context,"String_Node_Str" + function + "String_Node_Str"+ ft);
  FnID fid=context.defineFunction(function,ft,fdecl.defaultVals());
  tree.setIdentifier(fid);
  String pkg=Literals.extractLiteralString(context,tclPackage.child(0));
  String version=Literals.extractLiteralString(context,tclPackage.child(1));
  backend.requirePackage(new TclPackage(pkg,version));
  int pos=REQUIRED_CHILDREN;
  TclFunRef impl=null;
  if (pos < tree.getChildCount() && tree.child(pos).getType() == ExMParser.TCL_FUN_REF) {
    SwiftAST tclImplRef=tree.child(pos);
    String symbol=Literals.extractLiteralString(context,tclImplRef.child(0));
    impl=new TclFunRef(pkg,symbol,version);
    pos++;
  }
  TclOpTemplate inlineTcl=null;
  if (pos < tree.getChildCount() && tree.child(pos).getType() == ExMParser.INLINE_TCL) {
    SwiftAST inlineTclTree=tree.child(pos);
    inlineTcl=wrapper.loadTclTemplate(context,fid,fdecl,ft,inlineTclTree);
    pos++;
  }
  FunctionType backendFT=VarRepr.backendFnType(ft);
  backend.defineBuiltinFunction(fid,backendFT,inlineTcl,impl);
  context.getForeignFunctions().addForeignFunction(fid);
  for (; pos < tree.getChildCount(); pos++) {
    handleBuiltinFunctionAnnotation(context,fid,fdecl,tree.child(pos),inlineTcl != null);
  }
  ExecTarget taskMode=context.getForeignFunctions().getTaskMode(fid);
  boolean isTargetable=false;
  if (taskMode.isDispatched()) {
    isTargetable=true;
    context.setFunctionProperty(fid,FnProp.TARGETABLE);
  }
  if (impl != null) {
    context.setFunctionProperty(fid,FnProp.BUILTIN);
  }
 else {
    if (inlineTcl == null) {
      throw new UserException(context,"String_Node_Str" + "String_Node_Str" + fid.originalName());
    }
    context.setFunctionProperty(fid,FnProp.WRAPPED_BUILTIN);
    context.setFunctionProperty(fid,FnProp.SYNC);
    boolean isParallel=context.hasFunctionProp(fid,FnProp.PARALLEL);
    if (isParallel && (!taskMode.isAsync() || !taskMode.targetContext().isAnyWorkContext())) {
      throw new UserException(context,"String_Node_Str");
    }
    wrapper.saveWrapper(context,fid,backendFT,fdecl,taskMode,isParallel,isTargetable);
  }
}

Fixed version (ground truth):
private void defineBuiltinFunction(Context context,SwiftAST tree) throws UserException {
  final int REQUIRED_CHILDREN=5;
  assert(tree.getChildCount() >= REQUIRED_CHILDREN);
  String function=tree.child(0).getText();
  SwiftAST typeParamsT=tree.child(1);
  SwiftAST outputs=tree.child(2);
  SwiftAST inputs=tree.child(3);
  SwiftAST tclPackage=tree.child(4);
  assert(inputs.getType() == ExMParser.FORMAL_ARGUMENT_LIST);
  assert(outputs.getType() == ExMParser.FORMAL_ARGUMENT_LIST);
  assert(tclPackage.getType() == ExMParser.TCL_PACKAGE);
  assert(tclPackage.getChildCount() == 2);
  Set<String> typeParams=extractTypeParams(typeParamsT);
  FunctionDecl fdecl=FunctionDecl.fromAST(context,varCreator,exprWalker,function,inputs,outputs,typeParams);
  FunctionType ft=fdecl.getFunctionType();
  LogHelper.debug(context,"String_Node_Str" + function + "String_Node_Str"+ ft);
  FnID fid=context.defineFunction(function,ft,fdecl.defaultVals());
  tree.setIdentifier(fid);
  if (function.equals("String_Node_Str")) {
    System.err.println("String_Node_Str" + fid);
  }
  String pkg=Literals.extractLiteralString(context,tclPackage.child(0));
  String version=Literals.extractLiteralString(context,tclPackage.child(1));
  backend.requirePackage(new TclPackage(pkg,version));
  int pos=REQUIRED_CHILDREN;
  TclFunRef impl=null;
  if (pos < tree.getChildCount() && tree.child(pos).getType() == ExMParser.TCL_FUN_REF) {
    SwiftAST tclImplRef=tree.child(pos);
    String symbol=Literals.extractLiteralString(context,tclImplRef.child(0));
    impl=new TclFunRef(pkg,symbol,version);
    pos++;
  }
  TclOpTemplate inlineTcl=null;
  if (pos < tree.getChildCount() && tree.child(pos).getType() == ExMParser.INLINE_TCL) {
    SwiftAST inlineTclTree=tree.child(pos);
    inlineTcl=wrapper.loadTclTemplate(context,fid,fdecl,ft,inlineTclTree);
    pos++;
  }
  FunctionType backendFT=VarRepr.backendFnType(ft);
  backend.defineBuiltinFunction(fid,backendFT,inlineTcl,impl);
  context.getForeignFunctions().addForeignFunction(fid);
  for (; pos < tree.getChildCount(); pos++) {
    handleBuiltinFunctionAnnotation(context,fid,fdecl,tree.child(pos),inlineTcl != null);
  }
  ExecTarget taskMode=context.getForeignFunctions().getTaskMode(fid);
  boolean isTargetable=false;
  if (taskMode.isDispatched()) {
    isTargetable=true;
    context.setFunctionProperty(fid,FnProp.TARGETABLE);
  }
  if (impl != null) {
    context.setFunctionProperty(fid,FnProp.BUILTIN);
  }
 else {
    if (inlineTcl == null) {
      throw new UserException(context,"String_Node_Str" + "String_Node_Str" + fid.originalName());
    }
    context.setFunctionProperty(fid,FnProp.WRAPPED_BUILTIN);
    context.setFunctionProperty(fid,FnProp.SYNC);
    boolean isParallel=context.hasFunctionProp(fid,FnProp.PARALLEL);
    if (isParallel && (!taskMode.isAsync() || !taskMode.targetContext().isAnyWorkContext())) {
      throw new UserException(context,"String_Node_Str");
    }
    wrapper.saveWrapper(context,fid,backendFT,fdecl,taskMode,isParallel,isTargetable);
  }
}