I keep getting error: standard library error: _MaxBuiltinFloatType is not properly defined

When trying to compile my replacement version of the standard library.

But here's the thing, _MaxBuiltinFloatType is defined.

At the top of one of the files ("uSwift2.swift") i'm compiling to make my version of the standard library is this...

public typealias _MaxBuiltinFloatType = Builtin.FPIEEE64

public protocol _ExpressibleByBuiltinFloatLiateral {
  init(_builtinFloatLiteral value: _MaxBuiltinFloatType)
}

public protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType : _ExpressibleByBuiltinFloatLiteral
  init(floatLiteral value: FloatLiteralType)
}

@frozen
public struct Float {
  public // @testable
  var _value: Builtin.FPIEEE32

  @_transparent
  public init() {
    let zero: Int32 = 0
    self._value = Builtin.sitofp_Int32_FPIEEE32(zero._value)
  }

  @_transparent
  public // @testable
  init(_ _value: Builtin.FPIEEE32) {
    self._value = _value
  }
}

extension Float : _ExpressibleByBuiltinFloatLiteral {
  @_transparent
  public
  init(_builtinFloatLiteral value: Builtin.FPIEEE64) {
    self = Float(Builtin.fptrunc_FPIEEE64_FPIEEE32(value))
  }
}

public typealias FloatLiteralType = Float

public let test = 5.673

I'm compiling with swiftc -target avr-atmel-linux-gnueabihf -O -parse-stdlib -emit-module -module-name Swift -parse-as-library -primary-file uSwift2.swift ...rest of swift files being compiled..., most of it works, integer literals are interpreted correctly but I get the error...

uSwift2.swift:40:19: error: standard library error: _MaxBuiltinFloatType is not properly defined
public let test = 5.673

I built swiftc from my forked version of 5.1 where i've mostly just added support for AVR as a target and cherry picked some more recent llvm commits into the llvm tree. But nothing that should make any significant difference (https://github.com/carlos4242/swift/tree/avr-support-2). The "micro" standard library i'm building is a project i was working on last year then had to shelve and recently picked up again. Mostly it's stripped down and simplified from the normal stdlib files. I'm nearing completion getting it to compile, which will be fun. :slight_smile:

Looking at the code that emits this warning, it's in CSApply.cpp in visitFloatLiteralExpr, line 1994 in my codebase. It seems that the clause MaxFloatTypeDecl->hasInterfaceType() is returning false, which causes this error to be emitted.

However the error is hard for me to understand. The typealias does exist in my code and I don't understand the compiler code well enough to figure out what the real underlying problem is.

Any advice would be gratefully received!

Carl

That seems like a bug. Couple of things possibly worth trying:

  1. Try adding that typealias and the -target avr-atmel-linux-gnueabihf flag to test/NameLookup/stdlib.swift and run the test using something like ../llvm-project/llvm/utils/lit/lit.py -s -vv ../build/Ninja-ReleaseAssert/swift-macosx-x86_64/test-macosx-x86_64 --filter "NameLookup/stdlib.swift" (changing paths appropriately); at the moment, it tests for the diagnostic "standard library error: _MaxBuiltinFloatType is not properly defined" but that error should ideally go away (it does go away for me with x86_64 but I haven't tested avr). If the error does not go away, that means it's likely there's some target-specific strangeness (you can double-check this by changing the target for the uSwift2.swift example). If the error goes away, then there's something else that has gone wrong.
  2. You can try print-debugging by calling MaxFloatTypeDecl->dump().

Thank you very much, that looks like a good place to start my investigation.

I'll do the tests now, one question...

...did you mean NameBinding/stdlib.swift?

Yes, it's the same one. The test got renamed on Mar 27 in 5b99c2020f39526c6530b6137506e51fc6ab211b; I was looking at master, not at the AVR-specific branch.

So, I'm unable to run lit tests properly for some reason.

If I try to run lit, I get loads of errors about UNRESOLVED tests...

/Users/carlpeto/avr-swift/llvm-project/llvm/utils/lit/lit.py /Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/test-macosx-x86_64

gives

lit.py: /Users/carlpeto/avr-swift/swift/test/lit.cfg:244: note: using swift: /Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift
lit.py: /Users/carlpeto/avr-swift/swift/test/lit.cfg:244: note: using swiftc: /Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swiftc
lit.py: /Users/carlpeto/avr-swift/swift/test/lit.cfg:247: warning: couldn't find 'sil-opt' program, try setting SIL_OPT in your environment
...snip...
lit.py: /Users/carlpeto/avr-swift/llvm-project/llvm/utils/lit/lit/discovery.py:210: warning: test suite 'Swift-Unit' contained no tests
-- Testing: 4809 tests, 8 threads --
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/broken-swift-name.swift (1 of 4809)
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/basic.swift (2 of 4809)
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/obsoleted.swift (3 of 4809)
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/blocks.swift (4 of 4809)
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/properties.swift (5 of 4809)
UNRESOLVED: Swift(macosx-x86_64) :: APINotes/irgen-prop-getter-setter.swift (6 of 4809)
UNSUPPORTED: Swift(macosx-x86_64) :: AutolinkExtract/empty_archive.swift (7 of 4809)
UNSUPPORTED: Swift(macosx-x86_64) :: AutolinkExtract/empty.swift (8 of 4809)
UNSUPPORTED: Swift(macosx-x86_64) :: AutolinkExtract/error_no_inputs.swift (9 of 4809)
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 408, in _handle_results
    cache[job]._set(i, obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 577, in _set
    self._callback(self._value)
  File "/Users/carlpeto/avr-swift/llvm-project/llvm/utils/lit/lit/run.py", line 179, in consume_test_result
    self.display.update(test_with_result)
  File "/Users/carlpeto/avr-swift/llvm-project/llvm/utils/lit/lit/main.py", line 98, in update
    sys.stdout.flush()

So in order to get around this, I ran what I thought would be equivalent commands to what llvm-lit would run...

swift-macosx-x86_64/bin/swiftc -typecheck -emit-module -parse-stdlib -module-name Swift stdlib.swift

produces

/Users/carlpeto/avr-swift/swift/test/NameBinding/stdlib.swift:11:31: error: standard library error: _MaxBuiltinFloatType is not properly defined
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
                              ^

as expected, and it's the same error if I add -target avr-atmel-linux-gnueabihf.

Also if I use the installed "system" swift compiler from Xcode (version 5.1.3)...

/usr/bin/swiftc -typecheck -emit-module -parse-stdlib -module-name Swift stdlib.swift

the same error.

But here's the crazy thing...

If I change the test script like this...

// RUN: %target-typecheck-verify-swift -parse-stdlib -module-name Swift
  
enum Optional<T> {
  case none
  case some(T)
}

// <rdar://problem/15593704>
struct X {
  // This is in parse-stdlib mode with no default literal type.
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
}

typealias _MaxBuiltinFloatType = Builtin.FPIEEE64

protocol _ExpressibleByBuiltinFloatLiteral {
  static func _convertFromBuiltinFloatLiteral(
                value: _MaxBuiltinFloatType) -> Self
}

protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType : _ExpressibleByBuiltinFloatLiteral
  static func convertFromFloatLiteral(value: FloatLiteralType) -> Self
}

Then I still get the same error...

stdlib.swift:11:31: error: standard library error: _MaxBuiltinFloatType is not properly defined
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
                              ^

So I'm a bit confused what's going on. This is now using swiftc from a standard version of Xcode and I would not expect to see this error any more?

This is what I'm seeing for the output of this...

(lldb) e MaxFloatTypeDecl->dump()

( **typealias** range=[stdlib.swift:14:1 - line:14:42] "_MaxBuiltinFloatType" type='<<<unresolved>>>')

Just for reference, to save people having to hunt through source files and branches, the source code for that function is...

    Expr *visitFloatLiteralExpr(FloatLiteralExpr *expr) {
      // If the literal has been assigned a builtin float type,
      // don't mess with it.
      if (cs.getType(expr)->is<BuiltinFloatType>())
        return expr;

      auto &tc = cs.getTypeChecker();
      ProtocolDecl *protocol
        = tc.getProtocol(expr->getLoc(),
                         KnownProtocolKind::ExpressibleByFloatLiteral);
      ProtocolDecl *builtinProtocol
        = tc.getProtocol(expr->getLoc(),
                         KnownProtocolKind::ExpressibleByBuiltinFloatLiteral);

      // For type-sugar reasons, prefer the spelling of the default literal
      // type.
      auto type = simplifyType(cs.getType(expr));
      if (auto defaultType = tc.getDefaultType(protocol, dc)) {
        if (defaultType->isEqual(type))
          type = defaultType;
      }

      // Find the maximum-sized builtin float type.
      // FIXME: Cache name lookup.
      if (!MaxFloatTypeDecl) {
        SmallVector<ValueDecl *, 1> lookupResults;
        tc.getStdlibModule(dc)->lookupValue(/*AccessPath=*/{},
                                            tc.Context.Id_MaxBuiltinFloatType,
                                            NLKind::QualifiedLookup,
                                            lookupResults);
        if (lookupResults.size() == 1)
          MaxFloatTypeDecl = dyn_cast<TypeAliasDecl>(lookupResults.front());
      }
      if (!MaxFloatTypeDecl ||
          !MaxFloatTypeDecl->hasInterfaceType() ||
          !MaxFloatTypeDecl->getDeclaredInterfaceType()->is<BuiltinFloatType>()) {
        tc.diagnose(expr->getLoc(), diag::no_MaxBuiltinFloatType_found);
        return nullptr;
      }
      tc.validateDecl(MaxFloatTypeDecl);
      auto maxType = MaxFloatTypeDecl->getUnderlyingTypeLoc().getType();

      DeclName initName(tc.Context, DeclBaseName::createConstructor(),
                        { tc.Context.Id_floatLiteral });
      DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
                               { tc.Context.Id_builtinFloatLiteral });

      expr->setBuiltinType(maxType);
      return convertLiteralInPlace(
          expr, type, protocol, tc.Context.Id_FloatLiteralType, initName,
          builtinProtocol, builtinInitName, diag::float_literal_broken_proto,
          diag::builtin_float_literal_broken_proto);
    }

I'm not experienced enough to understand the dump output or what hasInterfaceType() does. Or how it gets set.

If I try to run lit, I get loads of errors about UNRESOLVED tests...

That's weird... I'm not entirely sure what's going on here but you might want to consider rebasing on top of a recent master (I'm assuming your goal is to eventually get this merged?) and try to get the tests working after this issue is resolved... Otherwise, you're gonna' have more trouble down the line as your branch gets more out-of-date.

So in order to get around this, I ran what I thought would be equivalent commands to what llvm-lit would run...

I think your command-line should be good enough, but FWIW, you can figure out the exact substitution by following target-typecheck-verify-swift in test/lit.cfg. At the moment, it looks like it passes swift -frontend -target <target> -Xcc --sysroot=<...> -typo-correction-limit 10 -typecheck -verify -disable-objc-attr-requires-foundation-module. In this particular case, I think you've got the relevant flags, so it doesn't matter so much, but maybe this is helpful in the future.

Then I still get the same error...

Is this "same error" only when you add -target avr-atmel-linux-gnueabihf or does it also happen with the default target?

This is now using swiftc from a standard version of Xcode and I would not expect to see this error any more?

It probably means that whatever is causing the problem in the locally built compiler (possibly a compiler bug) is also the same way in the toolchain.

My swift compiler is based on swift 5.1.5, which is pretty recent (from March). I took the most modern 5.1.x branch and forked it, because 5.2 and 5.3 are too much of a moving target and I've not got enough development resource to keep up with them yet. 5.1 is the basis for very recent and widely used Xcode so I would expect it to be fine. My changes are very small and shouldn't affect things.

I think llvm-lit not working is probably to do with how I invoked the build-script (I didn't add --test) and is probably fixable by deleting the build folder and rebuilding again from scratch, making sure tests are created. But your hint on how to find the underlying command line is great!

Because this is a major released version, my conclusion is that the standard library must have been compiled and released with it somehow, so either...

  1. there is something genuinely wrong with how my standard library is compiled and also with the stdlib.swift test you reference, but the compiler is not giving a full or useful error message, for example maybe the error message should be something more like "your _MaxBuiltinFloatType alias does not point to a type that is XXXX or YYYY so is not valid" but of course it is very rare for anyone to build anything other than the standard library that comes with the swift compiler, so there has never been a need to have a more accurate error message.

  2. the standard library code that is shipped with version 5.1.x happens to have the typealias and all connected protocols for Float and Double in a configuration that just happens to work "by luck" and because it's always worked, no one noticed that the parsing/semantic analysis is actually wrong.

Good points, it slipped my mind that 5.1 is still fairly recent.

I think this part is indicating the problem:

I suspect the RHS should be Builtin.FPIEEE64. Can you double-check if the same thing is happening without -target avr-atmel-linux-gnueabihf?

I think one of my earlier questions was missed, repeating that: what is the output when

  1. You compile the modified stdlib.swift (with the type alias) with -target avr-atmel-linux-gnueabihf and the locally build swift compiler.
  2. You compile the modified stdlib.swift (with the type alias) without -target avr-atmel-linux-gnueabihf and the locally build swift compiler.

You mention that " Then I still get the same error..." -- it's not clear to me which configuration(s) this is referring to.

I'll try to reproduce this in a day or two if I am able to. In the meantime, it might help to list the configurations and outputs in tabular form to make it easier to explain what is going on. Here's an example:

File Defines _MaxBuiltinFloatType -target setting Compiler Observations
stdlib.swift Yes avr-atmel-linux-gnueabihf Xcode 5.1 Write error message/dump output

Here is a summary of how I see it so far.

Compiler Target Test file Expected Result Actual Result
dev [^3] x86 original stdlib.swift error [^1] error
dev avr original stdlib.swift error error
5.1 [^4] x86 original stdlib.swift error error
dev x86 modified stdlib.swift [^2] no error error
dev avr modified stdlib.swift no error error
5.1 x86 modified stdlib.swift no error error

([^1]: the error is '_MaxBuiltinFloatType not properly defined'.)
([^2]: the modified file defines _MaxBuiltinFloatType with a typealias so it should compile without error.)
([^3]: the modified compiler is based on 5.1.5 source code with a small patch (see github link) to add avr as a target
([^4]: 5.1 compiler is from xcode, Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) Target: x86_64-apple-darwin18.7.0

Here are sample outputs from lldb debugging the compiler as it digests the modified version of stdlib.swift, which has _MaxBuiltinFloatType defined as Builtin.FPIEEE64 via a typealias so it should be compiling without error. I try with the x86 target and with the avr target...

Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ cat stdlib.swift 
// RUN: %target-typecheck-verify-swift -parse-stdlib -module-name Swift

enum Optional<T> {
  case none
  case some(T)
}

// <rdar://problem/15593704>
struct X {
  // This is in parse-stdlib mode with no default literal type.
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
}

typealias _MaxBuiltinFloatType = Builtin.FPIEEE64

protocol _ExpressibleByBuiltinFloatLiteral {
  static func _convertFromBuiltinFloatLiteral(
                value: _MaxBuiltinFloatType) -> Self
}

protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType : _ExpressibleByBuiltinFloatLiteral
  static func convertFromFloatLiteral(value: FloatLiteralType) -> Self
}
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ lldb -- /Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift -frontend -typecheck -primary-file stdlib.swift -disable-objc-attr-requires-foundation-module -target x86_64-apple-darwin18.7.0 -enable-objc-interop -parse-stdlib -color-diagnostics -module-name Swift
(lldb) target create "/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift"
Current executable set to '/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift' (x86_64).
(lldb) settings set -- target.run-args  "-frontend" "-typecheck" "-primary-file" "stdlib.swift" "-disable-objc-attr-requires-foundation-module" "-target" "x86_64-apple-darwin18.7.0" "-enable-objc-interop" "-parse-stdlib" "-color-diagnostics" "-module-name" "Swift"
(lldb) br s -f CSApply.cpp -l 1994
Breakpoint 1: where = swift`(anonymous namespace)::ExprRewriter::visitFloatLiteralExpr(swift::FloatLiteralExpr*) + 819 at CSApply.cpp:1994:9, address = 0x00000001016f6da3
(lldb) r
Process 45144 launched: '/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift' (x86_64)
Process 45144 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001016f6da3 swift`(anonymous namespace)::ExprRewriter::visitFloatLiteralExpr(this=0x00007ffeefbf5760, expr=0x0000000120011218) at CSApply.cpp:1994:9
   1979	
   1980	      // Find the maximum-sized builtin float type.
   1981	      // FIXME: Cache name lookup.
   1982	      if (!MaxFloatTypeDecl) {
   1983	        SmallVector<ValueDecl *, 1> lookupResults;
   1984	        tc.getStdlibModule(dc)->lookupValue(/*AccessPath=*/{},
   1985	                                            tc.Context.Id_MaxBuiltinFloatType,
   1986	                                            NLKind::QualifiedLookup,
   1987	                                            lookupResults);
   1988	        if (lookupResults.size() == 1)
   1989	          MaxFloatTypeDecl = dyn_cast<TypeAliasDecl>(lookupResults.front());
   1990	      }
   1991	      if (!MaxFloatTypeDecl ||
   1992	          !MaxFloatTypeDecl->hasInterfaceType() ||
   1993	          !MaxFloatTypeDecl->getDeclaredInterfaceType()->is<BuiltinFloatType>()) {
-> 1994	        tc.diagnose(expr->getLoc(), diag::no_MaxBuiltinFloatType_found);
   1995	        return nullptr;
   1996	      }
   1997	      tc.validateDecl(MaxFloatTypeDecl);
   1998	      auto maxType = MaxFloatTypeDecl->getUnderlyingTypeLoc().getType();
   1999	
   2000	      DeclName initName(tc.Context, DeclBaseName::createConstructor(),
   2001	                        { tc.Context.Id_floatLiteral });
   2002	      DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
   2003	                               { tc.Context.Id_builtinFloatLiteral });
   2004	
   2005	      expr->setBuiltinType(maxType);
   2006	      return convertLiteralInPlace(
   2007	          expr, type, protocol, tc.Context.Id_FloatLiteralType, initName,
   2008	          builtinProtocol, builtinInitName, diag::float_literal_broken_proto,
   2009	          diag::builtin_float_literal_broken_proto);
Target 0: (swift) stopped.
(lldb) e MaxFloatTypeDecl->dump()
(typealias range=[stdlib.swift:14:1 - line:14:42] "_MaxBuiltinFloatType" type='<<<unresolved>>>')
(lldb) q
Quitting LLDB will kill one or more processes. Do you really want to proceed: [Y/n] 
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ lldb -- /Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift -frontend -typecheck -primary-file stdlib.swift -disable-objc-attr-requires-foundation-module -target avr-atmel-linux-gnueabihf -enable-objc-interop -parse-stdlib -color-diagnostics -module-name Swift
(lldb) target create "/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift"
Current executable set to '/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift' (x86_64).
(lldb) settings set -- target.run-args  "-frontend" "-typecheck" "-primary-file" "stdlib.swift" "-disable-objc-attr-requires-foundation-module" "-target" "avr-atmel-linux-gnueabihf" "-enable-objc-interop" "-parse-stdlib" "-color-diagnostics" "-module-name" "Swift"
(lldb) br s -f CSApply.cpp -l 1994
Breakpoint 1: where = swift`(anonymous namespace)::ExprRewriter::visitFloatLiteralExpr(swift::FloatLiteralExpr*) + 819 at CSApply.cpp:1994:9, address = 0x00000001016f6da3
(lldb) r
Process 45149 launched: '/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift' (x86_64)
Process 45149 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001016f6da3 swift`(anonymous namespace)::ExprRewriter::visitFloatLiteralExpr(this=0x00007ffeefbf5760, expr=0x0000000121806418) at CSApply.cpp:1994:9
   1979	
   1980	      // Find the maximum-sized builtin float type.
   1981	      // FIXME: Cache name lookup.
   1982	      if (!MaxFloatTypeDecl) {
   1983	        SmallVector<ValueDecl *, 1> lookupResults;
   1984	        tc.getStdlibModule(dc)->lookupValue(/*AccessPath=*/{},
   1985	                                            tc.Context.Id_MaxBuiltinFloatType,
   1986	                                            NLKind::QualifiedLookup,
   1987	                                            lookupResults);
   1988	        if (lookupResults.size() == 1)
   1989	          MaxFloatTypeDecl = dyn_cast<TypeAliasDecl>(lookupResults.front());
   1990	      }
   1991	      if (!MaxFloatTypeDecl ||
   1992	          !MaxFloatTypeDecl->hasInterfaceType() ||
   1993	          !MaxFloatTypeDecl->getDeclaredInterfaceType()->is<BuiltinFloatType>()) {
-> 1994	        tc.diagnose(expr->getLoc(), diag::no_MaxBuiltinFloatType_found);
   1995	        return nullptr;
   1996	      }
   1997	      tc.validateDecl(MaxFloatTypeDecl);
   1998	      auto maxType = MaxFloatTypeDecl->getUnderlyingTypeLoc().getType();
   1999	
   2000	      DeclName initName(tc.Context, DeclBaseName::createConstructor(),
   2001	                        { tc.Context.Id_floatLiteral });
   2002	      DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
   2003	                               { tc.Context.Id_builtinFloatLiteral });
   2004	
   2005	      expr->setBuiltinType(maxType);
   2006	      return convertLiteralInPlace(
   2007	          expr, type, protocol, tc.Context.Id_FloatLiteralType, initName,
   2008	          builtinProtocol, builtinInitName, diag::float_literal_broken_proto,
   2009	          diag::builtin_float_literal_broken_proto);
Target 0: (swift) stopped.
(lldb) e MaxFloatTypeDecl->dump()
(typealias range=[stdlib.swift:14:1 - line:14:42] "_MaxBuiltinFloatType" type='<<<unresolved>>>')
(lldb) c
Process 45149 resuming
stdlib.swift:11:31: error: standard library error: _MaxBuiltinFloatType is not properly defined
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
                              ^
Process 45149 exited with status = 1 (0x00000001) 
(lldb) q
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ 
1 Like

Thanks for writing it out! These two lines are particularly interesting/weird. The baseline itself is off.

OTOH, if I run a modified stdlib.swift on top of master for x86, that error goes away:

/Users/varun/foss-swift/swift/test/NameLookup/stdlib.swift:13:54: error: incorrect message found
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                     type of expression is ambiguous without more context

Maybe something changed between March and now...

Looking at the code in master now, it looks like this definition is handled by a cleaner, generalised system in ASTContext.cpp. I can try putting my small patch on top of master and recompiling. The only problem is my standard library was written for 5.0 and updated to 5.1... the code in master is probably at least 5.3 and more like 6.0 so it may create a whole load of breakage on the rest of my standard library that would stop me from finding out if this was fixed.

I guess at the very least though I could then test this test case stdlib.swift against x86 and avr targets and re-run the test table I posted?

I guess at the very least though I could then test this test case stdlib.swift against x86 and avr targets and re-run the test table I posted?

Yes, that'd be great. Thanks!

OK, I've made some progress. Although I haven't fixed the problem in my custom stdlib, I have fixed the unit test you directed me to.

I haven't rebased on top of master yet as it will take a long time to do and I'm not sure it's the best use of time until I've eliminated all possible other routes to find out what's going wrong.

When I was debugging various runs through the compiler, I noticed that dumping typealias declarations often resulted in...

( **typealias** range=[stdlib.swift:14:1 - line:14:42] "XXX" type='<<<unresolved>>>')

...so I thought, maybe the error _MaxBuiltinFloatType is not properly defined will show up if the typealias is somehow not fully resolved at the point when it is tested or needed.

Looking in the standard library that comes with swift, _ExpressibleByBuiltinFloatLiteral and ExpressibleByFloatLiteral are defined in CompilerProtocols.swift... Float and Double and their conformances to those protocols are defined in FloatingPoint.swift and FloatingPointTypes.swift.gyb and all are declared before public typealias _MaxBuiltinFloatType = Builtin.FPIEEE64, which is in Policy.swift so is alphabetically last.

I rearranged the unit test in stdlib.swift to match this order, then added a struct that conformed to the protocols and updated the protocols.

It looks like the unit test might have been written a fairly long time ago when the _ExpressibleByBuiltinFloatLiteral for example needed a static factory function called _convertFromBuiltinFloatLiteral, but that has been changed to a simple initialiser in modern versions of the protocol. And similar for ExpressibleByFloatLiteral.

With all these changes, the script now runs without error when _MaxBuiltinFloatType is defined...

Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ cat stdlib.swift 
// RUN: %target-typecheck-verify-swift -parse-stdlib -module-name Swift

enum Optional<T> {
  case none
  case some(T)
}

public protocol _ExpressibleByBuiltinFloatLiteral {
  init(_builtinFloatLiteral value: _MaxBuiltinFloatType)
}

public protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType : _ExpressibleByBuiltinFloatLiteral
  init(floatLiteral value: FloatLiteralType)
}

public struct Y: ExpressibleByFloatLiteral, _ExpressibleByBuiltinFloatLiteral {
    public init(_builtinFloatLiteral value: _MaxBuiltinFloatType) {
    }
    public init(floatLiteral value: FloatLiteralType) {
    }
}

public typealias FloatLiteralType = Y

public typealias _MaxBuiltinFloatType = Builtin.FPIEEE64

// <rdar://problem/15593704>
struct X {
  // This is in parse-stdlib mode with no default literal type.
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
}
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ swift-macosx-x86_64/bin/swift -frontend -typecheck -primary-file stdlib.swift -disable-objc-attr-requires-foundation-module -target avr-atmel-linux-gnueabihf -enable-objc-interop -parse-stdlib -color-diagnostics -module-name Swift
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ echo $?
0
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ 

...and it produces the expected error when _MaxBuiltinFloatType is not properly defined...

Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ cat stdlib.swift 
// RUN: %target-typecheck-verify-swift -parse-stdlib -module-name Swift

enum Optional<T> {
  case none
  case some(T)
}

public protocol _ExpressibleByBuiltinFloatLiteral {
  init(_builtinFloatLiteral value: MaxBuiltinFloatType)
}

public protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType : _ExpressibleByBuiltinFloatLiteral
  init(floatLiteral value: FloatLiteralType)
}

public struct Y: ExpressibleByFloatLiteral, _ExpressibleByBuiltinFloatLiteral {
    public init(_builtinFloatLiteral value: MaxBuiltinFloatType) {
    }
    public init(floatLiteral value: FloatLiteralType) {
    }
}

public typealias FloatLiteralType = Y

public typealias MaxBuiltinFloatType = Builtin.FPIEEE64

// <rdar://problem/15593704>
struct X {
  // This is in parse-stdlib mode with no default literal type.
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
}
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ swift-macosx-x86_64/bin/swift -frontend -typecheck -primary-file stdlib.swift -disable-objc-attr-requires-foundation-module -target avr-atmel-linux-gnueabihf -enable-objc-interop -parse-stdlib -color-diagnostics -module-name Swift
stdlib.swift:31:31: error: standard library error: _MaxBuiltinFloatType is not properly defined
  var _maxLoadFactorInverse = 1.0 // expected-error{{standard library error: _MaxBuiltinFloatType is not properly defined}}
                              ^
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ echo $?
1
Carls-MacBook-Air:Ninja-DebugAssert carlpeto$ 

So it looks like the order of things in the standard library by luck happens to avoid this problem, but it's fragile, reorder things a bit and float literals break. Like you say, this looks like a bug, although of course it won't affect 99.99% of swift users, who don't like to make their own standard libraries (everyone else in the world except me?) :slight_smile:

Unfortunately my stdlib is still reporting the same error, even after I did the same reordering so I'm going to have to try more tricks. Within the file where ExpressibleByFloatLiteral, _ExpressibleByBuiltinFloatLiteral and _MaxBuiltinFloatType are defined, float literals now work fine in my library. But in any other files in my library, they can refer to all these definitions but float literals do not work and produce the error described in this post.

Terms of Service

Privacy Policy

Cookie Policy