tinysun212 / swift-windows

Swift compiler for Cygwin, MSVC, MinGW. Full development environment can be downloaded from the Swift for Windows.
http://SwiftForWindows.github.io
Apache License 2.0
331 stars 30 forks source link

[MinGW] Strange behaviour with C structures initialisation #6

Open GunGraveKoga opened 7 years ago

GunGraveKoga commented 7 years ago

Windows structure WIN32_FIND_DATA:

typedef struct _WIN32_FIND_DATA {
  DWORD    dwFileAttributes;
  FILETIME ftCreationTime;
  FILETIME ftLastAccessTime;
  FILETIME ftLastWriteTime;
  DWORD    nFileSizeHigh;
  DWORD    nFileSizeLow;
  DWORD    dwReserved0;
  DWORD    dwReserved1;
  TCHAR    cFileName[MAX_PATH];
  TCHAR    cAlternateFileName[14];
} WIN32_FIND_DATA, *PWIN32_FIND_DATA, *LPWIN32_FIND_DATA;

If I try to init this structure inside swift code like this:

let win_find_data = WIN32_FIND_DATA()

I get output:

$ swift -v -I. ./main.swift
Swift version 3.0-dev (LLVM 449f1d0641, Clang 0ebffe65d9, Swift 9555952109)
Target: x86_64-w64-windows-gnu
"C:\\msys64\\swift\\bin\\swift.exe" -frontend -interpret ./main.swift -target x86_64-w64-windows-gnu -disable-objc-interop -I . -color-diagnostics -module-name main
Segmentation fault

If I use name of structure with underscore

let win_find_data = _WIN32_FIND_DATA()

I get:

$ swift -v -I. ./main.swift
Swift version 3.0-dev (LLVM 449f1d0641, Clang 0ebffe65d9, Swift 9555952109)
Target: x86_64-w64-windows-gnu
"C:\\msys64\\swift\\bin\\swift.exe" -frontend -interpret ./main.swift -target x86_64-w64-windows-gnu -disable-objc-interop -I . -color-diagnostics -module-name main
#0 0x0000000000b63619 swift::FunctionType::get(swift::Type, swift::Type, swift::AnyFunctionType::ExtInfo const&) (C:\msys64\swift\bin\swift.exe+0x763619)
#1 0x0000000000c4a312 swift::ParameterList::getFullInterfaceType(swift::Type, llvm::ArrayRef<swift::ParameterList*>, swift::DeclContext*) (C:\msys64\swift\bin\swift.exe+0x84a312)
#2 0x000000000096e0d1 swift::ClangImporter::Implementation::createConstant(swift::Identifier, swift::DeclContext*, swift::Type, swift::Expr*, swift::ConstantConvertKind, bool, swift::ClangNode) (C:\msys64\swift\bin\swift.exe+0x56e0d1)
#3 0x000000000096de07 swift::ClangImporter::Implementation::createConstant(swift::Identifier, swift::DeclContext*, swift::Type, clang::APValue const&, swift::ConstantConvertKind, bool, swift::ClangNode) (C:\msys64\swift\bin\swift.exe+0x56de07)
#4 0x000000000098c89c createMacroConstant(swift::ClangImporter::Implementation&, clang::MacroInfo const*, swift::Identifier, swift::DeclContext*, swift::Type, clang::APValue const&, swift::ConstantConvertKind, bool, swift::ClangNode) (C:\msys64\swift\bin\swift.exe+0x58c89c)
#5 0x000000000098c5c6 importNumericLiteral(swift::ClangImporter::Implementation&, swift::DeclContext*, clang::MacroInfo const*, swift::Identifier, clang::Token const*, clang::Token const&, clang::MacroInfo const*, clang::QualType const*) (C:\msys64\swift\bin\swift.exe+0x58c5c6)
#6 0x000000000098adc8 importMacro(swift::ClangImporter::Implementation&, swift::DeclContext*, swift::Identifier, clang::MacroInfo const*, clang::MacroInfo const*, clang::QualType*) (C:\msys64\swift\bin\swift.exe+0x58adc8)
#7 0x000000000098a539 swift::ClangImporter::Implementation::importMacro(swift::Identifier, clang::MacroInfo*) (C:\msys64\swift\bin\swift.exe+0x58a539)
#8 0x0000000000953e80 swift::ClangImporter::Implementation::lookupValue(swift::SwiftLookupTable&, swift::DeclName, swift::VisibleDeclConsumer&) (C:\msys64\swift\bin\swift.exe+0x553e80)
#9 0x0000000000953543 swift::ClangImporter::Implementation::lookupVisibleDecls(swift::SwiftLookupTable&, swift::VisibleDeclConsumer&) (C:\msys64\swift\bin\swift.exe+0x553543)
#10 0x00000000009533e0 swift::ClangModuleUnit::lookupVisibleDecls(llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::VisibleDeclConsumer&, swift::NLKind) const (C:\msys64\swift\bin\swift.exe+0x5533e0)
#11 0x0000000000c3423d swift::ModuleDecl::lookupVisibleDecls(llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::VisibleDeclConsumer&, swift::NLKind) const (C:\msys64\swift\bin\swift.exe+0x83423d)
#12 0x0000000000c3eb4c void lookupInModule<llvm::DenseMap<swift::Identifier, std::pair<swift::namelookup::ResolutionKind, llvm::SmallSet<swift::CanType, 4u, (anonymous namespace)::SortCanType> >, llvm::DenseMapInfo<swift::Identifier>, llvm::detail::DenseMapPair<swift::Identifier, std::pair<swift::namelookup::ResolutionKind, llvm::SmallSet<swift::CanType, 4u, (anonymous namespace)::SortCanType> > > >, swift::namelookup::lookupVisibleDeclsInModule(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::NLKind, swift::namelookup::ResolutionKind, swift::LazyResolver*, swift::DeclContext const*, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >)::$_1>(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::namelookup::ResolutionKind, bool, swift::LazyResolver*, llvm::SmallDenseMap<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*>, llvm::TinyPtrVector<swift::ValueDecl*>, 32u, llvm::DenseMapInfo<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >, llvm::detail::DenseMapPair<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*>, llvm::TinyPtrVector<swift::ValueDecl*> > >&, swift::DeclContext const*, bool, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >, swift::namelookup::lookupVisibleDeclsInModule(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::NLKind, swift::namelookup::ResolutionKind, swift::LazyResolver*, swift::DeclContext const*, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >)::$_1) (C:\msys64\swift\bin\swift.exe+0x83eb4c)
#13 0x0000000000c3f11c void lookupInModule<llvm::DenseMap<swift::Identifier, std::pair<swift::namelookup::ResolutionKind, llvm::SmallSet<swift::CanType, 4u, (anonymous namespace)::SortCanType> >, llvm::DenseMapInfo<swift::Identifier>, llvm::detail::DenseMapPair<swift::Identifier, std::pair<swift::namelookup::ResolutionKind, llvm::SmallSet<swift::CanType, 4u, (anonymous namespace)::SortCanType> > > >, swift::namelookup::lookupVisibleDeclsInModule(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::NLKind, swift::namelookup::ResolutionKind, swift::LazyResolver*, swift::DeclContext const*, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >)::$_1>(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::namelookup::ResolutionKind, bool, swift::LazyResolver*, llvm::SmallDenseMap<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*>, llvm::TinyPtrVector<swift::ValueDecl*>, 32u, llvm::DenseMapInfo<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >, llvm::detail::DenseMapPair<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*>, llvm::TinyPtrVector<swift::ValueDecl*> > >&, swift::DeclContext const*, bool, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >, swift::namelookup::lookupVisibleDeclsInModule(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::NLKind, swift::namelookup::ResolutionKind, swift::LazyResolver*, swift::DeclContext const*, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >)::$_1) (C:\msys64\swift\bin\swift.exe+0x83f11c)
#14 0x0000000000c3e9a3 swift::namelookup::lookupVisibleDeclsInModule(swift::ModuleDecl*, llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, llvm::SmallVectorImpl<swift::ValueDecl*>&, swift::NLKind, swift::namelookup::ResolutionKind, swift::LazyResolver*, swift::DeclContext const*, llvm::ArrayRef<std::pair<llvm::ArrayRef<std::pair<swift::Identifier, swift::SourceLoc> >, swift::ModuleDecl*> >) (C:\msys64\swift\bin\swift.exe+0x83e9a3)
#15 0x0000000000c2844c swift::lookupVisibleDecls(swift::VisibleDeclConsumer&, swift::DeclContext const*, swift::LazyResolver*, bool, swift::SourceLoc) (C:\msys64\swift\bin\swift.exe+0x82844c)
#16 0x0000000000a66494 swift::TypeChecker::performTypoCorrection(swift::DeclContext*, swift::DeclRefKind, swift::Type, swift::DeclName, swift::SourceLoc, swift::OptionSet<swift::NameLookupFlags, unsigned int>, swift::LookupResult&, unsigned int) (C:\msys64\swift\bin\swift.exe+0x666494)
#17 0x0000000000a187a3 (C:\msys64\swift\bin\swift.exe+0x6187a3)
#18 0x0000000000a224e6 (anonymous namespace)::PreCheckExpression::walkToExprPre(swift::Expr*) (C:\msys64\swift\bin\swift.exe+0x6224e6)
#19 0x0000000000baa5b3 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) (C:\msys64\swift\bin\swift.exe+0x7aa5b3)
#20 0x0000000000ba644b swift::Expr::walk(swift::ASTWalker&) (C:\msys64\swift\bin\swift.exe+0x7a644b)
#21 0x0000000000a18ccf swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) (C:\msys64\swift\bin\swift.exe+0x618ccf)
#22 0x0000000000a1e9b5 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) (C:\msys64\swift\bin\swift.exe+0x61e9b5)
#23 0x0000000000a1f926 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*) (C:\msys64\swift\bin\swift.exe+0x61f926)
#24 0x0000000000a1fabc swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int) (C:\msys64\swift\bin\swift.exe+0x61fabc)
#25 0x0000000000a33404 validatePatternBindingDecl(swift::TypeChecker&, swift::PatternBindingDecl*, unsigned int) (C:\msys64\swift\bin\swift.exe+0x633404)
#26 0x0000000000a2f9bf (anonymous namespace)::DeclChecker::visit(swift::Decl*) (C:\msys64\swift\bin\swift.exe+0x62f9bf)
#27 0x0000000000a2f8ff swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) (C:\msys64\swift\bin\swift.exe+0x62f8ff)
#28 0x0000000000a8a4d2 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (C:\msys64\swift\bin\swift.exe+0x68a4d2)
#29 0x0000000000a8a09b swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) (C:\msys64\swift\bin\swift.exe+0x68a09b)
#30 0x0000000000a4f248 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) (C:\msys64\swift\bin\swift.exe+0x64f248)
#31 0x000000000083b891 swift::CompilerInstance::performSema() (C:\msys64\swift\bin\swift.exe+0x43b891)
#32 0x00000000004353d2 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) (C:\msys64\swift\bin\swift.exe+0x353d2)
#33 0x000000000043428e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) (C:\msys64\swift\bin\swift.exe+0x3428e)
#34 0x0000000000403892 main (C:\msys64\swift\bin\swift.exe+0x3892)
#35 0x00000000004013ed __tmainCRTStartup C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt\crtexe.c:336:0
#36 0x000000000040152b mainCRTStartup C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt\crtexe.c:214:0
#37 0x00000000775559bd BaseThreadInitThunk (C:\Windows\system32\kernel32.dll+0x159bd)
#38 0x000000007778a2e1 RtlUserThreadStart (C:\Windows\SYSTEM32\ntdll.dll+0x2a2e1)
Segmentation fault

Meanwhile other C structures can be initialized without errors

let socket_address = sockaddr_in()

Output:

Swift version 3.0-dev (LLVM 449f1d0641, Clang 0ebffe65d9, Swift 9555952109)
Target: x86_64-w64-windows-gnu
"C:\\msys64\\swift\\bin\\swift.exe" -frontend -interpret ./main.swift -target x86_64-w64-windows-gnu -disable-objc-interop -I . -color-diagnostics -module-name main
tinysun212 commented 7 years ago

Thanks for your report. Could you tell me the imported module and the full source of main.swift ?

tinysun212 commented 7 years ago

This is my test source in Cygwin.

$ cat main.swift
import CWin32

let win_find_data = WIN32_FIND_DATA()
print(win_find_data)

$ cat Package.swift
import PackageDescription

let package = Package(
    name: "Issue6",
    dependencies: [
        .Package(url: "https://github.com/tumasgiu/CWin32.git", majorVersion: 1, minor: 0)
    ]
 )

To build with the Swift Package Manager, Cygwin needs some more options (Sadly, the swiftpm is not yet ported for MinGW.)

$ swift build -Xswiftc -I/usr/include/w32api -Xswiftc -I/usr/include
Compile Swift Module 'Issue6' (1 sources)
...
Linking ./.build/debug/Issue6

$ ./.build/debug/Issue6
_WIN32_FIND_DATAA(dwFileAttributes: 0, ftCreationTime: __C._FILETIME(dwLowDateTime: 0, dwHighDateTime: 0), ftLastAccessTime: __C._FILETIME(dwLowDateTime: 0, dwHighDateTime: 0), ftLastWriteTime: __C._FILETIME(dwLowDateTime: 0, dwHighDateTime: 0), nFileSizeHigh: 0, nFileSizeLow: 0, dwReserved0: 0, dwReserved1: 0, cFileName: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), cAlternateFileName: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

I think @tumasgiu 's CWin32 module will be helpful (or good reference) for MinGW version.

GunGraveKoga commented 7 years ago

Source code of my main.swift is similar to yours cygwin test. I use CWin32 too, and on cygwin it hasn't errors, only on mingw. Actually I get error even if used my own C structure, similar to WIN32_FIND_DATA, defined in CWin32.h header

tinysun212 commented 7 years ago

Could you try this again with the latest Swift compiler for MinGW-w64?

tinysun212 commented 6 years ago

Could you try this again with the latest compiler? This issue is very similar to SwiftForWindows/SwiftForWindows#32.

tinysun212 commented 6 years ago

ping?